如何修复'TypeError:System.config不是函数'

时间:2019-04-15 13:29:05

标签: typescript systemjs

我正在学习TypeScript,今天我安装了SystemJS,以便可以导入一些文件。首先,我仅将需要运行的主文件(即app.js)导入index.html

<script src="node_modules/systemjs/dist/system.js"></script>
<script>
     System.import('app.js');
</script>

但是我得到了这个错误: https://i.stack.imgur.com/lz7Hm.png

所以我将html代码变成了:

<script src="node_modules/systemjs/dist/system.js"></script>
<script>
    System.config({
        baseURL: '/',
        packages: {
            "/ts": {
                defaultExtension: 'js'
            }
        }
    });
    System.import('app.js');
</script>

现在我收到此错误:https://i.stack.imgur.com/RS0k7.png

package.json:

{
  "name": "ts",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {
    "jquery": "^3.4.0",
    "systemjs": "^3.1.2",
    "typescript": "^3.4.2"
  },
  "devDependencies": {
    "lite-server": "^2.4.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "lite-server"
  },
  "author": "",
  "license": "ISC"
}

app.ts:console.log("Hello")

我被困在这一点上。任何帮助,将不胜感激。预先感谢。

2 个答案:

答案 0 :(得分:1)

找到 system.js 并将System.config()放入其中,这样可以解决错误。

<script src="node_modules/systemjs/dist/system.js">
        System.config({
        baseURL: "/",
        packages: {
          "/": {
            defaultJSExtensions: true
          }
        }
      })
      System.import("app.js")</script>
<script>

答案 1 :(得分:0)

我想您会错误地升级到最新的system.js ... SystemJS 2.0不支持System.Config 请参阅https://guybedford.com/systemjs-2.0

它可以使用https://github.com/systemjs/systemjs/blob/2.0.0/docs/package-name-maps.md

完成

如果您现在设法更新了代码,不妨在这里发布更新的示例,以供所有人使用。

相关问题