2017-07-29 131 views
1

我得到角度路由錯誤Angular 2不能匹配路由錯誤

未捕獲(承諾):錯誤:無法匹配任何路由。網址細分:'null' 錯誤:無法匹配任何路線。 URL段:「null'at ApplyRedirects.webpackJsonp .../../../router/@angular/router.es5.js.ApplyRedirects.noMatchError

下面是我的路線對象:

export const ROUTES: Routes = [ 
    { path: '', component: TrendsComponent}, 
    { path: 'trends', component: TrendsComponent } 
]; 

我正在使用@ angular/router 4.0.0

回答

1

嘗試按以下方式設置路由,使用redirectTo在啓動時將用戶路由到路徑「/ trends」。

確保您將RouterModule導入到您的NgModule中。對於根模塊或類似的模塊,您可以使用RouterModule.forRoot(ROUTES)以及使用RouterModule.forChild(ROUTES)的功能模塊。請務必在根目錄和任何功能模塊中包含<router-outlet></router-outlet>

const ROUTES: Routes = [ 
    { path: 'trends', component: TrendsComponent }, 
    { path: '', redirectTo: '/trends', pathMatch: 'full' } 
]; 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <div> 
     <h2>Hello World</h2> 
     <p>Redirect to path /trends component is occurring on startup</p> 
     <router-outlet></router-outlet> 
    </div> 
    `, 
}) 
export class App {} 

@NgModule({ 
    imports: [ BrowserModule, RouterModule.forRoot(ROUTES) ], 
    declarations: [ App, TrendsComponent ], 
    bootstrap: [ App ] 
}) 
export class AppModule {} 

這是一個plunker演示的功能。

更新:plunker已更新爲顯示聲明和導入子路由和模塊(非延遲加載)。

希望有幫助。