typescript - 如何使用 TypeScript 模块 :system export with systemjs in browser

标签 typescript systemjs

我正在尝试使用 systemjs,并且我已经使用 tsc 导出了这段代码: https://github.com/quantumjs/solar-popup/blob/master/package.json#L10

{
  "compilerOptions": {
    "module": "system",
    "target": "es5",
    "sourceMap": true,
    "outFile":"./build/solar-popup.js",
    "sourceRoot": "./src/SolarPopup.ts",
    "rootDir": "./src/"

  },
  "exclude": [
    "node_modules"
  ]
}

然而,当试图在浏览器中使用它时,导入的对象不包含任何导出。我认为这是由于 solar-popup.js 不包含任何导出,仅包含 System.register 调用

enter image description here

导出看起来像这样:

System.register("SolarPopup", ["ModalBackground", "constants"], function (exports_4, context_4) {
    "use strict";
    var __moduleName = context_4 && context_4.id;
    var ModalBackground_1, constants_2, SolarPopup;
    return {
        setters: [
            function (ModalBackground_1_1) {
                ModalBackground_1 = ModalBackground_1_1;
            },
            function (constants_2_1) {
                constants_2 = constants_2_1;

等等

最佳答案

当你在 tsconfig.json 中有这两个选项时

"module": "system",
"outFile":"./build/solar-popup.js",

TypeScript 生成包含多个 System.register 调用的单个输出文件,每个调用都使用自己的名称注册一个模块:

 System.register("SolarPopup", ["ModalBackground", "constants"], function ...

“SolarPopup”在这里是一个模块名称。 SystemJS 将此类文件解释为 bundle, not a module .

导入包的结果是一个空对象,副作用是其中的所有模块都已注册并且可以立即导入而无需获取它们。

因此您需要额外的导入才能从包中获取模块。试试这个:

System.import('../build/solar-popup.js').then(function() {
    System.import('SolarPopup').then(function(SolarPopup) {
        console.dir(SolarPopup);
    });
});

关于typescript - 如何使用 TypeScript 模块 :system export with systemjs in browser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41161080/

相关文章:

javascript - TypeScript Array<T> 继承

javascript - 如何执行某些功能

angularjs - 导入 angular-mocks 的正确方法是什么

typescript - Angular 2 HTTP "Cannot resolve all parameters for ' AppService'”

javascript - Angular 4路由添加组件而不是替换它

jquery - 如何使用 TypeScript/jQuery 传递元素

javascript - Jest - 在一个测试套件中多次测试模块

typescript - 如何根据文件扩展名而不是内容使 SystemJS 转译 'typescript'

javascript - 通过 systemjs 请求的脚本失败并显示 401 Unauthorized

Angularjs typescript 迁移