angularjs - tsconfig 模块选项 - 'System' 是指 SystemJS 吗?

标签 angularjs angular typescript module

在 tsconfig.json 文件中,可以将以下选项指定为 compilerOptions 'module' 属性的值:

System

这样我们得到:

{
  "compilerOptions": {
    "module": "System",
     ...

System 是否引用 SystemJS(即,如果 SystemJS 被用作模块加载器,我在创建 AngularJS 或 Angular 应用程序时是否总是需要在我的 tsconfig.json 中使用“System”)?文档似乎没有对此进行解释:

https://www.typescriptlang.org/docs/handbook/compiler-options.html

在 TypeScript 的 Visual Studio 项目配置中,“TypeScript 构建 > 模块系统”下还有一个“系统”选项,这显然与 tsconfig.json 中的“系统”指的是同一事物。

最佳答案

是的,它指的是 SystemJS,如果您希望 TypeScript 编译器按照您的预期运行并生成您期望的代码,那么明确包含它很重要。如果您不指定,TypeScript 将恢复为基于当前 target ES 版本(默认 ES3,触发 commonjs模块)。正如编译器文档中所述,它会影响其他默认编译器选项,例如 moduleResolutionallowSyntheticDefaultImports

例如,如果你有一个如下的 typescript 文件

import { functionA } from './moduleA';
functionA();

如果您将 module 指定为 system,您生成的代码将使用系统调用:

System.register(["./moduleA"], function (exports_1, context_1) {
    "use strict";
    var __moduleName = context_1 && context_1.id;
    var moduleA_1;
    return {
        setters: [
            function (moduleA_1_1) {
                moduleA_1 = moduleA_1_1;
            }
        ],
        execute: function () {
            moduleA_1.functionA('Stuff');
        }
    };
});

但是,如果您允许编译器默认为 commonjs,它将生成:

"use strict";
var moduleA_1 = require('./moduleA');
moduleA_1.functionA('Stuff');

请注意,生成的代码可能因 TS 版本或其他标志而异。

来自测试/经验和 module resolutioncompiler您链接的文档。

关于angularjs - tsconfig 模块选项 - 'System' 是指 SystemJS 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48142665/

相关文章:

css - Angular UI 选项卡,在移动设备中为假

angular - 既然 Angular 是双向数据绑定(bind),为什么我应该在 Angular 中使用 Redux?

javascript - 使用 Electron 和 Angular 2 开发桌面应用程序

typescript - 在 TypeScript 中推断函数参数

javascript - UI Bootstrap 自定义指令 Controller 不是一个函数

javascript - 如何将 AngularJS 范围对象转换为简单的 JS 数组?

javascript - 如何循环遍历所有对象数组?

angular - 调用 ngx-bootstrap 函数的 typescript

angular - ng2-charts 中的堆积条形图

javascript - Angular 1.6 组件绑定(bind)返回未定义