配置 SystemJS 以加载我的 Angular 2 组件

Configure SystemJS to load my Angular 2 component

我正在从 ng1 升级到 ng2。我添加了 Angular 2 并成功导入了它的模块:

<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>
<script src="/node_modules/angular2/bundles/http.dev.js"></script>
<script src="/node_modules/angular2/bundles/router.dev.js"></script>

我添加了以下配置:

 <script>
        System.config({
          packages: {
            app: {
              format: 'cjs',
              defaultExtension: 'js'
            }
          }
        });

        System.import('scripts/bootstrap.js').then(null, console.error.bind(console));

 </script>

现在我正在尝试添加我的第一个 ng2 组件/模块并导入它:

使用TypeScript编写的组件:

import {Component} from 'angular2/core';

@Component({
  selector: 'my-component',
  templateUrl: 'app/components/my-component/my-component.html',
  styleUrls: ['app/components/my-component/my-component.css'],
  providers: [],
  directives: [],
  pipes: []
})
export default class MyComponent {

  constructor() {}

}

正在导入我的组件:

import MyComponent from './components/my-component/my-component';

组件的ES5编译代码为:

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
    switch (arguments.length) {
        case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
        case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
        case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
    }
};
var core_1 = require('angular2/core');
var MyComponent = (function () {
    function MyComponent() {
    }
    MyComponent = __decorate([
        core_1.Component({
            selector: 'my-component',
            templateUrl: 'app/components/my-component/my-component.html',
            styleUrls: ['app/components/my-component/my-component.css'],
            providers: [],
            directives: [],
            pipes: []
        })
    ], MyComponent);
    return MyComponent;
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = MyComponent;

结果是寻找

的404错误

http://localhost:9000/scripts/components/my-component/my-component

现在,我明白我应该:

  1. 使用 script 标签加载我的组件文件,类似于我对 Angular2 包所做的。这以 JS 错误结束:required 未定义。这是因为我的文件 bundled 不正确。
  2. 配置 SystemJS / Typescript 这样它就知道加载我的模块而不用向我的 html.
  3. 添加脚本标签

我在这里错过了什么?

在您的情况下,系统配置中的包应该是 'scripts'。它应该与文件夹名称匹配。因为您将其命名为 'app',所以它不会将 defaultExtension 'js' 添加到模块文件名

<script>
        System.config({
          packages: {
            scripts: {    // <--- right there
              format: 'cjs',
              defaultExtension: 'js'
            }
          }
        });

        System.import('scripts/bootstrap.js').then(null, console.error.bind(console));

 </script>