0

I'm creating an AngularJS 2 application for the first time and I'm trying to implement routing. I'm adapting the example on the developer guide of the official AngularJS 2 documentation website to a simple Product CRUD application but I can't seem to make it work. I'm always getting this error:enter image description here I've double checked everything at least a dozen times, but I can't find what's wrong or what's missing. My code:

index.html

<html>

<head>
  <!-- Set the base href -->
  <base href=".">
  <!-- <script>document.write('<base href="' + document.location + '" />');   </script> -->
  <title>Angular 2 QuickStart</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="styles.css">
  <!-- 1. Load libraries -->
  <!-- Polyfill(s) for older browsers -->
  <script src="node_modules/core-js/client/shim.min.js"></script>
  <script src="node_modules/zone.js/dist/zone.js"></script>
  <script src="node_modules/reflect-metadata/Reflect.js"></script>
  <script src="node_modules/systemjs/dist/system.src.js"></script>
  <!-- JQuery -->
  <script src="node_modules/jquery/dist/jquery.min.js"></script>
  <!-- Twitter Bootstrap -->
  <link href="node_modules/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
  <!-- 2. Configure SystemJS -->
  <script src="systemjs.config.js"></script>
  <script>
      System.import('app').catch(function(err){ console.error(err); });
  </script>
</head>
<!-- 3. Display the application -->

<body>
  <my-app>loading...</my-app>
</body>

</html>

maint.ts

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import { appRouterProviders } from './app.routes';

bootstrap(AppComponent, [
    appRouterProviders
]).catch(err => console.error(err));

app.component.ts

import { Component } from '@angular/core';
import { HTTP_PROVIDERS } from '@angular/http';
import 'rxjs/Rx';
import { ROUTER_DIRECTIVES } from '@angular/router';

import { NavigationBarComponent } from './shared/navigation-bar/navigation-bar.component';
import { FooterComponent } from './shared/footer/footer.component';

import { ProductComponent } from './products/product.component';
import { ProductService } from './products/product.service';

@Component({
    selector: 'my-app',
    template: `
    <navigation-bar></navigation-bar>
    <a [routerLink]="['products']">Products</a>

    <store-products></store-products>
    <footer></footer>
    <router-outlet><router-outlet> 
    `
    ,
    directives: [
        ROUTER_DIRECTIVES,
        NavigationBarComponent,
        FooterComponent,
        ProductComponent

     ],
    providers: [
        ProductService,
        HTTP_PROVIDERS
    ]
})
export class AppComponent { }

app.route.ts

import { provideRouter, RouterConfig } from '@angular/router';
import { ProductComponent } from './products/product.component';

const routes: RouterConfig = [
  { path: 'products', component: ProductComponent }
];

export const appRouterProviders = [
  provideRouter(routes)
];

product.component.ts

import { Component, OnInit }  from '@angular/core';

import { Product } from './product';
import { ProductService } from './product.service'

@Component({
    selector: 'store-products',
    moduleId: module.id,
    templateUrl: 'product.html',
    styleUrls: ['product.css']

})
export class ProductComponent implements OnInit {

    products: Product[]
    errorMessage: string

    constructor(private productService: ProductService) {

    }

    ngOnInit(): void {
        this.productService.getProducts()
            .subscribe(
            products => this.products = products,
            error => this.errorMessage = <any>error);
    }
}

systemjs.config.js

 /**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
    // map tells the System loader where to look for things
    var map = {
        'app': 'app', // 'dist',
        '@angular': 'node_modules/@angular',
        'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
        'rxjs': 'node_modules/rxjs'
    };
    // packages tells the System loader how to load when no filename and/or no extension
    var packages = {
        'app': { main: 'main.js', defaultExtension: 'js' },
        'rxjs': { defaultExtension: 'js' },
        'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
    };
    var ngPackageNames = [
        'common',
        'compiler',
        'core',
        'forms',
        'http',
        'platform-browser',
        'platform-browser-dynamic',
        'router',
        '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' };
    }
    // 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);

package.json

{
"name": "angular2-quickstart",
"version": "1.0.0",
"scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
},
"license": "ISC",
"dependencies": {
    "@angular/common": "2.0.0-rc.3",
    "@angular/compiler": "2.0.0-rc.3",
    "@angular/core": "2.0.0-rc.3",
    "@angular/forms": "0.1.1",
    "@angular/http": "2.0.0-rc.3",
    "@angular/platform-browser": "2.0.0-rc.3",
    "@angular/platform-browser-dynamic": "2.0.0-rc.3",
    "@angular/router": "3.0.0-alpha.7",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "2.0.0-rc.3",
    "systemjs": "0.19.27",
    "core-js": "^2.4.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "^0.6.12",
    "angular2-in-memory-web-api": "0.0.12",
    "bootstrap": "^3.3.6"
},
"devDependencies": {
    "concurrently": "^2.0.0",
    "lite-server": "^2.2.0",
    "typescript": "^1.8.10",
    "typings": "^1.0.4"
}
}

Does anyone have any clue of what could be wrong?

1 Answer 1

1

Can you show me your systemjs.config.js file and package .json

just add this line in systemjs.config.js

ngPackageNames.forEach(setPackageConfig);

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

    var config = {
        map: map,
        packages: packages
    };

It will solve your issue :)

Sign up to request clarification or add additional context in comments.

6 Comments

In which part of the file should I add these lines?
just add the line i have mentioned with ----> between the lines shown in your systemjs.config.js
add it at last 7th line of systemjs.config.js file
Thank you so much, it works now! It kind boggles my mind as to why they don't explain this in the developer guide. Was I suppose to guess you had to add this line? Seriously, smh
Could you please explain why I have to add this line to make it work?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.