簡體   English   中英

Angular平台服務器(通用):使用get參數進行路由

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM