Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

Static method call not working in typescript

The static access in my following example does not modify the html document as expected and I don't understand why. I am new to typescript. Compilations were successful.

File Main.ts:

import { BabylonApplication } from './BabylonApplication';
BabylonApplication.test(); //<-- this static access yields no result

File BabylonApplication.ts:

import * as BABYLON from 'babylonjs';

export class BabylonApplication {
    public static test() {
        document.getElementById("demo").innerHTML = "Hello World.";
    }
    //..some out commented stuff where i need the BABYLON import
}

The index.html:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Babylon Template</title>

    <script src="https://cdn.babylonjs.com/babylon.js"></script>
    <script src="https://cdn.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>

    <script src="built/SimpleJavaScriptClass.js"></script>
    <script src="built/BabylonApplication.js"></script>

</head>

<body>

    <p id="demo">test</p>

    <canvas id="renderCanvas"></canvas>

    <script src="built/Main.js"></script>

</body>

</html>

I noticed that when I write simple javascript classes with no export definition and import definition the static access is working.

For instance, the following plain javascript static method call is working

SimpleJavaScriptClass.ts:

class SimpleJavaScriptClass{

    static test() {
        document.getElementById("demo").innerHTML = "Hello World from javascript";
    }

}

Adjusted Main.ts:

//import { BabylonApplication } from './BabylonApplication';
//BabylonApplication.test();
SimpleJavaScriptClass.test();

So the simple javascript calls are fine. I have the suspicion that it has something to do with the import definition. I used npm install --save babylonjs on the project (no, I don't confuse babylonjs with babel).

What am I doing wrong in typescript? Or what is wrong with the import?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...