javascript - Angular 2 RC5 : Ingen leverantör för Router


Jag har ett problem med att använda den nya Angular 2 RC5-routern (routerversionen är RC1).

Här är loggen jag får från utvecklarkonsolen:

EXCEPTION: Error in /templates/app.component.html:2:0
ORIGINAL EXCEPTION: No provider for Router!

Här är hur min app.modules.ts ser ut:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { AppComponent }   from './components/app.component';
import { routing } from './app.routes';

@NgModule({
    declarations: [AppComponent],
    imports:      [BrowserModule, RouterModule, FormsModule, HttpModule, routing],
    bootstrap:    [AppComponent],
})
export class AppModule {}

Här är min boot.ts-fil:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

Min app.routes.ts-fil...

import { Routes, RouterModule } from '@angular/router';

// Components
import { HomeComponent } from './components/home.component';

const routes: Routes = [
    // Root
    { path: '/', name: 'Home', component: HomeComponent, useAsDefault: true},
];

// - Updated Export
export const routing = RouterModule.forRoot(routes); 

...och min fil app.component.ts:

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

@Component({
    selector: 'app',
    templateUrl: '/templates/app.component.html'
})

export class AppComponent {
    viewContainerRef: any;

    public constructor(viewContainerRef:ViewContainerRef) {
        // You need this small hack in order to catch application root view container ref
        this.viewContainerRef = viewContainerRef;
    }
}

Är det något jag missar här?

Svar #1

Inuti filen app.routes.ts:

Lägg till detta importtillstånd

import { ModuleWithProviders } from '@angular/core';

Än

export const routing: ModuleWithProviders = RouterModule.forRoot(routes);

Detta borde fungera.

Relaterade artiklar