2017-02-17 113 views
2

我已經失去了7天以上,但無法使其工作。當我將路徑放入children中的模板時,出現錯誤,應用程序崩潰。Angular 2路由 - 無法匹配URL段中的任何路由

這裏是我的代碼:

export const routes: Routes = [ 
{path: 'store', component: StoreComponent, 
    children: [ 
      {path: 'apple', component: AppleComponent} 
    ] 
} 

AppleComponent.ts

import {Component, OnInit, Type} from '@angular/core'; 
import {Router, ActivatedRoute} from "@angular/router"; 

@Component ({ 
    selector: 'apple', 
    templareUrl: '../apple.html 
}) 

export class AppleComponent implements OnInit { 
    constructor(){} 

    nGOnInit():void{} 
} 

我已經把<base href="/"> index.html文件中,但沒有幫助。

我剛開始學習Angular2,所以請原諒我,如果我問一個愚蠢的問題。

編輯:這將需要去children,因爲router-outlet

謝謝你們。

回答

1

您可以簡單地使用這個

{ path: 'store/apple', component: AppleComponent, pathMatch: 'full' } 

另外,您可以用這種方式,

export const storeChildrenRoutes: Routes = [ 
    { 
    path: 'store', 
    component: StoreComponent, 
    children: [ 
     { 
     path: 'apple', 
     component: AppleComponent, 
     } 
    ] 
    } 
]; 
@NgModule({ 
    imports: [ 
    RouterModule.forChild(storeChildrenRoutes) 
    ], 
    exports: [ 
    RouterModule 
    ] 
}) 
export class StoreChildRoutingModule {} 

有此作爲一個獨立的模塊,並導入此到AppRouting模塊。 你AppRouting模塊將包含你的主要路線有

RouterModule.forRoot(appRootRoutes) 
+0

是的我可以,但我需要它,因爲路由器插座的孩子。 –

+0

你在'StoreComponent'裏面使用 Aravind

+0

不,你會怎麼做到這一點? –