Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t在AMD中使用typescript时设置输入/初始模块_Javascript_Typescript_Module_Requirejs_Amd - Fatal编程技术网

Javascript Can';t在AMD中使用typescript时设置输入/初始模块

Javascript Can';t在AMD中使用typescript时设置输入/初始模块,javascript,typescript,module,requirejs,amd,Javascript,Typescript,Module,Requirejs,Amd,假设我有一个主模块,它应该是我页面上所有其他必要模块的起点。此模块是主模块.ts 我还有另一个格式化数字的模块,那就是数字格式化程序.ts。它位于services文件夹中 我希望Typescript编译器从main module.ts开始编译一个文件,并导入所需的所有内容 它会这样做,但生成的脚本不会执行主模块。正确的方法应该是将主模块作为匿名模块生成,这样,requirejs将尽快执行它。但是它给主模块起了一个名字,这会破坏一切,因为从来没有调用过任何模块 什么是匿名模块? 这是一个没有名字的

假设我有一个主模块,它应该是我页面上所有其他必要模块的起点。此模块是
主模块.ts

我还有另一个格式化数字的模块,那就是
数字格式化程序.ts
。它位于
services
文件夹中

我希望Typescript编译器从
main module.ts
开始编译一个文件,并导入所需的所有内容

它会这样做,但生成的脚本不会执行
主模块
。正确的方法应该是将
主模块作为匿名模块生成,这样,requirejs将尽快执行它。但是它给主模块起了一个名字,这会破坏一切,因为从来没有调用过任何模块

什么是匿名模块? 这是一个没有名字的模块。例如:

define(['i-need-this', 'i-also-need-this'], function(){
    // I do something here...
});
如果模块有一个名称,它将只在被调用时执行,但如果它是匿名的,它将尽快执行

具有名称的模块示例:

define('this-is-my-name', ['i-need-this', 'i-also-need-this'], function(){
        // I do something here...
    });
仅当需要(
require(['this-is-my-name'])
)或需要的其他模块将此模块作为依赖项时,才会执行上述模块

源代码 主模块 数字格式化程序.ts tsconfig.json 生成的代码 别发疯生成的代码中有很多内容,其中大部分只是将所有内容转换为ES5。这里重要的是主模块被命名为
main module
。如果我们只是去掉这个名字,一切都会正常工作

script.js
var uu assign=(this&&this.u assign)| | Object.assign | |函数(t){
for(var s,i=1,n=arguments.length;i
import { NumberFormatter } from '../services/number-formatter';

alert(
    NumberFormatter.toCurrency(
        NumberFormatter.round(19284.18, {roundBy: 10})
    )
);
export class NumberFormatter {

    static round(value: number, options?: {roundBy: number}): number {

        let defaultOptions: {roundBy: number} = {
            roundBy: 1};
        options = {...defaultOptions, ...options};

        return Math
            .round(value / options.roundBy) * options.roundBy;
    }

    static toCurrency(value: number, options?: {thousandsSeparator: string}): string {

        let defaultOptions: {thousandsSeparator: string} = {
            thousandsSeparator: '.'};
        options = {...defaultOptions, ...options};

        return value
            .toFixed(2)
            .toString()
            .replace(/\B(?=(\d{3})+(?!\d))/g, ";")
            .replace(/\./g, ",")
            .replace(/\;/g, options.thousandsSeparator);
    }

}
{
    "compilerOptions": {
        "lib": [
            "dom",
            "es5",
            "scripthost",
            "es2015.iterable"
        ],
        "target": "es5",
        "outFile": "dist/script.js",
        "module": "AMD",
        "noEmitOnError": false,
        "noImplicitAny": true
    },
    "include": [
        "src/scripts/main-module.ts"
    ]
}
var __assign = (this && this.__assign) || Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
        s = arguments[i];
        for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
            t[p] = s[p];
    }
    return t;
};
define("services/number-formatter", ["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var NumberFormatter = /** @class */ (function () {
        function NumberFormatter() {
        }
        NumberFormatter.round = function (value, options) {
            var defaultOptions = {
                roundBy: 1
            };
            options = __assign({}, defaultOptions, options);
            return Math
                .round(value / options.roundBy) * options.roundBy;
        };
        NumberFormatter.toCurrency = function (value, options) {
            var defaultOptions = {
                thousandsSeparator: '.'
            };
            options = __assign({}, defaultOptions, options);
            return value
                .toFixed(2)
                .toString()
                .replace(/\B(?=(\d{3})+(?!\d))/g, ";")
                .replace(/\./g, ",")
                .replace(/\;/g, options.thousandsSeparator);
        };
        return NumberFormatter;
    }());
    exports.NumberFormatter = NumberFormatter;
});
define("main-module", ["require", "exports", "services/number-formatter"], function (require, exports, number_formatter_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    alert(number_formatter_1.NumberFormatter.toCurrency(number_formatter_1.NumberFormatter.round(19284.18, { roundBy: 10 })));
});