2017-08-24 55 views
0

我試圖用我的platform-server/universal應用程序中的參數來描述路由,但目前爲止沒有任何成功。角度平臺服務器(通用):使用獲取參數的路由

任何人都有一個想法如何定義實現?

根據我從express路由知道我嘗試以下,但我最終面臨的一個錯誤

routes.ts:

export const ROUTES: string[] = [ 
    '/', 
    '/item/:id' 
]; 

main.server.ts:

ROUTES.forEach((route: string) => { 
    app.get(route, (req: Request, res: Response) => { 
    res.render('../dist/index', { 
     req: req, 
     res: res 
    }); 
    }); 
}); 

我的組件:

constructor(private route: ActivatedRoute) { 

    } 

ngOnInit() { 
    this.sub = this.route.params.subscribe(params => { 
     console.log(params['id']); 
    }); 
} 

有了這個代碼,服務器npm start開始沒有錯誤,但是當我打電話http://localhost:8000/item/thisistheparam我的臉在瀏覽器控制檯

Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'item/thisistheparam'

回答

0

哎呀我發現這個問題下面的錯誤,我忘了路徑添加到我的懶惰模塊太多; )

分別在我item.module.ts:

@NgModule({ 
    declarations: [ItemView], 
    imports: [ 
    RouterModule.forChild([ 
     { path: ':id', component: PublicItemView, pathMatch: 'full'} 
    ]) 
    ] 
}) 

我改變path: ''path: ':id'和一切,如果工作就像一個魅力現在