0

I am setting up routing in Angular 2, and everything is the same based on the documentation from their website.

Here is an example of my import in my app.component.ts

import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router';

@Component({
  selector: 'my-app',
  templateUrl: 'app/templates/html/app.component.html',
  directives: [ROUTER_DIRECTIVES]
})

export class AppComponent{

}

Unfortunately I am getting this error.

enter image description here

I look at my file tree, and I noticed that there is no bundles folder inside @angular/router

enter image description here

Is this a bug? How can I fix this? Thanks!

KB_
  • 2,113
  • 2
  • 26
  • 28
Mix Austria
  • 925
  • 2
  • 15
  • 35

2 Answers2

2

This is from you systemjs file.

First you can map the ng2 router module, similar to your app:

var map = {
    <...>
    '@angular/router':            'node_modules/@angular/router',
    <...>
};

Next set manual the packIndex for it:

packages['@angular/router'] = { main: 'index.js', defaultExtension: 'js' };

To fix this error adapt the code below to your particular case.

Note the different ng2 modules and versions which you want to use and your app name (change if appropriate):

(function(global) {
    // map tells the System loader where to look for things
    var map = {
        'YourApp':                    'YourApp', // 'dist',
        '@angular':                   'node_modules/@angular',
        '@angular/router':            'node_modules/@angular/router',
        'rxjs':                       'node_modules/rxjs'
    };
    // packages tells the System loader how to load when no filename and/or no extension
    var packages = {
        'YourApp':                    { main: 'app.js',  defaultExtension: 'js' },
        'rxjs':                       { defaultExtension: 'js' }
    };
    var ngPackageNames = [
        'common',
        'forms',
        'compiler',
        'core',
        'http',
        'platform-browser',
        'platform-browser-dynamic',
        'router-deprecated',
        'upgrade',
    ];
    // Individual files (~300 requests):
    function packIndex(pkgName) {
        packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
    }
    // Bundled (~40 requests):
    function packUmd(pkgName) {
        packages['@angular/'+pkgName] = { main: 'bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
    }

    packages['@angular/router'] = { main: 'index.js', defaultExtension: 'js' };

    // Most environments should use UMD; some (Karma) need the individual index files
    var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
    // Add package entries for angular packages
    ngPackageNames.forEach(setPackageConfig);
    var config = {
        map: map,
        packages: packages
    };
    System.config(config);
})(this);
Gabi
  • 589
  • 3
  • 9
1

The router package doesn't have a bundle at the moment.

So check your systemjs config. I quess you forgot the following line:

// No umd for router yet
packages['@angular/router'] = { main: 'index.js', defaultExtension: 'js' };

See also https://github.com/alexzuza/angular2-typescript-systemjs/blob/master/systemjs.config.js#L40

yurzui
  • 205,937
  • 32
  • 433
  • 399