加载模块未捕获ReferenceError:未定义SystemJS

时间:2019-10-01 07:14:25

标签: typescript systemjs

我需要加载一个模块,尽管它在网络上很普遍,并且我尝试了一些解决方案,但似乎无法解决该错误:

我有一个这样的班级:

export class Example{
    public Name: string;
    public Id: number;   
}

将类导入app.js文件:

import { Example } from "./exampleClass";

let example = new Example();

example.Name = "Hello World";

document.write(example.Name);

尝试过:

首先在tsconfig中

"module": "system"

<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<script src="node_modules/systemjs/dist/system.js"></script> 
</head>
<body>
    <script>
     SystemJS.config({
         baseURL: '/',
         defaultJSExtensions: true
     });
     SystemJS.import('./dist/app.js');
    </script>
</body>
</html>

这:

    <script>
     SystemJS.config({
         baseURL: '/',
         packages: {

            '/': {
                defaultJSExtensions: 'js'
            }
         }
     });
     SystemJS.import('./dist/app.js');
    </script>

1 个答案:

答案 0 :(得分:0)

使用方法“ System.import()”代替方法“ SystemJS.config()”。例如,请参见HTML文件“ index.html”文件:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Learning TypeScript</title>
        <script src='node_modules/systemjs/dist/system.js'></script>
    </head>
    <body>
        <div id="app">Change it with jQuery</div>
        <script>
            System.import('./app.js');
        </script>
    </body>
</html>

请确保在“ package.json”文件中包含以下代码,因为您将SystemJS用作模块加载器:

"dependencies": {
  "systemjs": "^6.1.2"
},

请确保在TypeScript配置文件“ tsconfig.json”文件中包含以下代码,因为您将SystemJS用作模块加载器:

"compilerOptions": {
    "module": "system",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */