簡體   English   中英

如何將typescript項目與html集成

[英]How to integrate typescript project with html

我想從HTML正文中調用一個打字稿函數。 但是,我無法從HTML調用我的函數(D2O_main()),因為我不完全理解生成的js代碼。 任何幫助是極大的贊賞。

我使用以下命令生成了js:tsc --module umd main.ts

這是html文件。

<head>
    <script type="text/javascript" src="main.js"></script>
</head>
<body onload="D2O_main();">
    <canvas id="canvas-001"></canvas>
</body>
</html>

這是main.ts文件:

import {Init} from "./Core/IO/Init"
let init:Init;
function D2O_main() {
    init = new Init("canvas-001");
}

這是生成的main.js文件:

(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "./Core/IO/Init"], factory);
    }
})(function (require, exports) {
    "use strict";
    exports.__esModule = true;
    var Init_1 = require("./Core/IO/Init");
    var init;
    function D2O_main() {
        init = new Init_1.Init("canvas-001");
    }
});

我在瀏覽器控制台上收到以下錯誤:

未捕獲的ReferenceError:在onload上未定義D2O_main

再一次,非常感謝任何幫助。

這是因為嵌套函數是函數作用域的 - 你需要將function D20_main移動到它所包含的function D20_main之外,從而有效地使它成為一個全局可訪問的函數:

(function (factory) {...})(function (require, exports) {...}); 
function D20_main() {...}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM