2016-12-28 13 views
1
@RouteConfig([ 
    { path: '/post/:post_id', component: PostComponent } 
]) 

この古典的なルート戦略/post/1715を使用して、このJSONデータを受け取りました。角度2のパス変数

{ 
    "post_id": 1715, 
    "title": "The Post Title", 
    "urlTitle": "the-post-title", 
    "body": "Blah, blah ... blah" 
} 

しかし、どのようにリンクにurlTitleを追加することによって、URLのスタイル/post/1715/the-post-titleをStackOverflowのようにルータをリダイレクトするには?

答えて

7
@RouteConfig([ 
     { path: 'post/:id', component: PostRedirect }, 
     { path: 'post/:id/:title', component: PostComponent } 
]) 

最初のコンポーネントはidを読み取り、次にタイトルを持つ2番目のコンポーネントにリダイレクトします。

完全サンプル:http://plnkr.co/edit/5BSzvpOH2kAfPAUnJj4O?p=preview