简体   繁体   English

使用TypeScript的Angular:ReferenceError:系统未定义System.config

[英]Angular with TypeScript: ReferenceError: System is not defined System.config

I tried to install Angular 2 for TypeScript according to this tutorial: https://angular.io/guide/quickstart 我根据本教程尝试为TypeScript安装Angular 2: https//angular.io/guide/quickstart

And I am getting this error: 我收到这个错误:

ReferenceError: System is not defined System.config ReferenceError:系统未定义System.config

I don't know how this happens. 我不知道这是怎么回事。

the folder structure: 文件夹结构:

project
|-index.hml
|-assets
    |-js
    |- jquery
    |-app
       |-app.component.js
       |-app.component.ts
       |-app.component.js.map
       |-main.js
       |-main.ts
       |-main.js.map

You need to have SystemJS included into your HTML page. 您需要在您的HTML页面中包含SystemJS。 To make work your Angular2 application from your node_modules folder, you need at least: 要从node_modules文件夹中使用Angular2应用程序,至少需要:

<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script> <!---
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>

And configure SystemJS to load your compiled TypeScript (actually JavaScript one with a js extension). 并配置SystemJS以加载已编译的TypeScript(实际上是带有js扩展名的JavaScript)。 Something like that: 像这样的东西:

<script>
  System.config({
    map: {
      app: 'assets/js/app'
    },
    packages: {        
      app: {
        format: 'register',
        defaultExtension: 'js'
      }
    }
  });
</script>

This configuration means that when you try to import some modules starting with app/ , SystemJS will load corresponding JS file (compiled from TypeScript one). 此配置意味着当您尝试导入以app/开头的某些模块时,SystemJS将加载相应的JS文件(从TypeScript编译)。 For example: System.import('app/main'); 例如: System.import('app/main'); will load the app/main.js file. 将加载app/main.js文件。

This means that you need to have compiled your TypeScript files before. 这意味着您需要先编译TypeScript文件。 When launching the npm run start command, the tsc compiler is automatically started in background and will compile TypeScript files into JS ones when changes will be detected. 启动npm run start命令时,tsc编译器会在后台自动启动,并在检测到更改时将TypeScript文件编译为JS文件。 You can check that the compiler is actually started and you have the JS files created... 您可以检查编译器是否已实际启动并且您已创建JS文件...

index.html 的index.html

make sure to add following. 确保添加以下内容。

<script src="https://code.angularjs.org/2.0.0-beta.7/angular2-polyfills.js"></script>


<script src="https://code.angularjs.org/tools/system.js"></script>
// error reason can be missing of this reference.


<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.7/Rx.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.7/angular2.min.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.7/http.min.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.7/router.dev.js"></script>

This is a duplicate for angular2js: Uncaught Reference Error: System is not defined 这是angular2js的重复:未捕获的参考错误:未定义系统

In case of Angular2 Seed already implemented injections and required libs loading mechanism you should just use those methods. 如果Angular2 Seed已经实现了injections并且需要libs loading机制,那么您应该使用这些方法。

In case of you are creating app from scratch you can include libs as you want as described above. 如果您scratch创建应用程序,您可以根据需要包含libs,如上所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM