我正在从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
现在,我明白我应该:
script
标记加载我的组件文件,类似于我对Angular2包所做的操作。这最终导致JS错误:required
未定义。这是因为我的文件不正确bundled
。 我在这里缺少什么?
答案 0 :(得分:7)
系统配置中的软件包应该是&#39;脚本&#39;在你的情况下。它应该与文件夹名称匹配。因为您将其命名为&#39; app&#39;,所以它不会添加defaultExtension&#39; js&#39;到模块文件名
<script>
System.config({
packages: {
scripts: { // <--- right there
format: 'cjs',
defaultExtension: 'js'
}
}
});
System.import('scripts/bootstrap.js').then(null, console.error.bind(console));
</script>