[英]Angular Platform-Server Respond with custom http status code
[英]Angular Platform-server (Universal): route with get params
我正在嘗試在我的platform-server
/ universal
應用程序中描述帶有get參數的路線,但到目前為止沒有任何成功。
任何人都有一個想法如何定義實現?
根據我從express
路由了解到的內容,我嘗試了以下操作,但最終遇到錯誤
route.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
我在瀏覽器控制台中遇到以下錯誤
錯誤:未捕獲(承諾):錯誤:無法匹配任何路由。 網址段:“ item / thisistheparam”
糟糕,我發現了問題,我也忘記將路徑添加到我的惰性模塊;)
分別在我的item.module.ts中:
@NgModule({
declarations: [ItemView],
imports: [
RouterModule.forChild([
{ path: ':id', component: PublicItemView, pathMatch: 'full'}
])
]
})
我現在將path: ''
更改為path: ':id'
以及一切都像魅力一樣工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.