Recently, I made the switch from ng1 to ng2. I successfully imported Angular 2 and its modules into my project:
<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>
In addition, I included the following configuration settings:
<script>
System.config({
packages: {
app: {
format: 'cjs',
defaultExtension: 'js'
}
}
});
System.import('scripts/bootstrap.js').then(null, console.error.bind(console));
</script>
Now I am in the process of adding my first ng2 component/module and importing it.
The component is written using 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() {}
}
Importing my component:
import MyComponent from './components/my-component/my-component';
The resulting ES5 compiled code for the component is as follows:
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;
Upon execution, a 404 error occurs while attempting to retrieve:
http://localhost:9000/scripts/components/my-component/my-component
In troubleshooting this issue, I have determined that I need to either:
- Load my component file using a
script
tag, similar to how I imported Angular2 bundles. However, this approach results in a JS error stating thatrequired
is undefined due to improper bundling. - Configure SystemJS/Typescript so that my module can be loaded without the need for a script tag in the HTML.
What could I potentially be overlooking or missing in this scenario?