SystemJS:在提供默认的JS扩展时我缺少什么

时间:2017-07-10 14:17:17

标签: javascript systemjs

不,我没有构建angular2应用程序。 我正在尝试SystemJS,这是我加载模块的脚本

<script type="text/javascript">
    System.config({ baseURL: '/scripts/ts',
        map: { 'deck': 'deck'}, defaultJSExtensions: "js", defaultExtension: "js" });
    System.import('deck').then(function () {
            document.write("Loaded...");
        });
</script>

以下是我收到的控制台消息。

system.src.js:sourcemap:80 Uncaught (in promise) Error: Unexpected identifier   Evaluating http://localhost:61968/scripts/ts/deck   Loading deck
    at eval (<anonymous>)
    at evaluate (system.src.js:sourcemap:2821)
    at system.src.js:sourcemap:3620
    at dynamicExecute (system.src.js:sourcemap:1144)
    at doEvaluate (system.src.js:sourcemap:1091)
    at ensureEvaluate (system.src.js:sourcemap:999)
    at system.src.js:sourcemap:617
    at <anonymous>

看起来systemjs没有获取默认扩展名。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

defaultJSExtensions ceased to be supported from version 0.20.0 onwards. You can see it in the release notes for 0.20.0-alpha1: "Removes support for defaultJSExtensions."

defaultExtension is a parameter for packages defined in the packages option so you have to define a package in order to use it.

Considering that your map is also not necessary, then you should use something like this:

{ 
  baseURL: '/scripts/ts',
  packages: {
    // Yep, this defines a package with an empty name. This will
    // encompass everything not encompassed by a more specific package
    // name.
    "": {
      defaultExtension: "js" 
    },
  },
}

Actually, this also would work:

{ 
  baseURL: '/scripts/ts',
  packages: {
    "": {},
  },
}

Here's the reason for it: there's no global default for adding extensions. However, if you define a package but you do not specify a defaultExtension for it, then the package automatically gets a default value for defaultExtension which is "js".