2016-08-16 67 views
8

我正面臨使用新的Angular 2 RC5路由器(路由器版本爲RC1)的問題。Angular 2 RC5:沒有路由器的供應商

這裏是日誌我從開發者控制檯獲得:

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

這裏是我的app.modules.ts是什麼樣子:

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

這裏是我的boot.ts文件:

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

platformBrowserDynamic().bootstrapModule(AppModule); 

我app.routes.ts文件...

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

...和我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; 
    } 
} 

有我丟失的東西嗎?

+1

「useAsDefault」不再是有效的屬性。我還會刪除path屬性中的'/',並將添加到index.html文件中。 –

+1

路由「名稱」也需要刪除。 – SudarP

回答

9

app.routes.ts文件裏面:

添加此導入狀態

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

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

這應該工作。

+0

嘿@EmmanuelScarabin添加ModuleWithProviders沒有爲我解決。我有'沒有供應商的路由器!'只要我在app.component.html – SudarP

+1

@SudarP中添加標籤,就應該檢查您是否在任何組件中沒有使用「router-deprecated」。這是造成這個問題的第二個原因。 –

+0

@EmmanuelScarabin,非常感謝,這固定了它。 – SudarP

相關問題