Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure SystemJS to load my Angular 2 component

I am upgrading from ng1 to ng2. I added Angular 2 and successfully imported its modules:

<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>

I added the following config:

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

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

 </script>

Now I am trying to add my first ng2 component/ module and to import it:

The component it 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';

And component's ES5 compiled code is:

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;

The result is a 404 error looking for

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

Now, I understand that I should either:

  1. Load my component file using script tag, similar to what I did with Angular2 bundles. This ends up with a JS error: required is undefined. This is because my file is not bundled correctly.
  2. Config SystemJS / Typescript so it will know to load my module without adding a script tag to my html.

What am I missing here?

like image 433
Yaniv Efraim Avatar asked Mar 13 '23 19:03

Yaniv Efraim


1 Answers

The packages in the System config should be 'scripts' in your case. It should match the folder name. Because you have it named 'app', it does not add the defaultExtension 'js' to the modules filename

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

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

 </script>
like image 112
Poul Kruijt Avatar answered Mar 16 '23 07:03

Poul Kruijt