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

问题描述 投票:0回答:1

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

我有这样的课程:

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>
typescript systemjs
1个回答
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'. */
© www.soinside.com 2019 - 2024. All rights reserved.