簡體   English   中英

為Angular2構建自定義RouteLink?

[英]Building custom RouteLink for Angular2?

/posts/b/1從ID為1的Blog加載帖子

/posts/b/-1加載所有博客中的所有帖子

如何配置使用/posts posts從所有博客加載所有帖子的路由?

我使用了重定向到RouterModule.forRoot但是[routerLinkActive]有問題

路線的順序很重要,請找到以下示例

  RouterModule.forRoot([
      {path:'products',component:ProductListComponent},
      {path:'products/:id',
      canActivate:[ProductDetailGuard],
      component:ProductDetailComponent
    },
      {path:'welcome',component:WelcomeComponent},
      {path:'',redirectTo:'welcome',pathMatch:'full'},
      {path:'**',redirectTo:'welcome',pathMatch:'full'}


    ])
  ]

您可以使用: this.router.navigate()進行重定向

在應該使用/posts/b/{id}處理路線的組件中,您可以使用ActivatedRoute獲取當前路線參數並采取相應的措施。

你應該做這樣的事情:

constructor(protected route: ActivatedRoute) {}

void checkRoute() {
    route.params.Subscribe(
        params => {
            let id = params['id']; // use the name of your route parameter
            if (!(id > 0)) {
                // redirect here with router.navigate()
            }
        }
    );
 }

免責聲明:尚未檢查此確切代碼,對不起,如果語法/錯字。

可能需要在構造函數中隨時調用函數checkRoute()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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