2016-05-13 3 views
2

一部のユーザーが招待状を使用して自分のWebアプリケーションに入ります。そのため、次のようなリンクが表示されます。https://example.com/invitaion/12345ここで、12345は固有の招待番号です。角2 RC1:使用された最初のURLからパラメータを取得

ユーザがリンクをクリックし、私のAppComponentがフレームワークによってクライアント側で初期化された場合、使用されたURLが「招待/ *」であったという事実をどのようにして取得できますか?

答えて

0

この変数にアクセスするには、RouteParamsを使用し、コンポーネント内で使用することをお勧めします。

コンポーネント自体の中に今
// let's assume you labeled it as `path : '/invitation/:id'` in your @Route setup. 

@Route([ 
    { path : '/invitation/:id', component : MyComponent } 
]) 

AppComponentで新しいルータ(> = RC.0)で

import { RouteParams } from '@angular/router'; 

// Now simply get the ID when injecting RouteParams within your constructor 

class MyComponent { 
    id: string; 
    constructor(_params: RouteParams) { 
     this.id = _params.get('id'); 
    } 
} 
+0

それが動作しない場合は、@ angle/router-deprecated'からRouteParamsをインポートする必要があるかもしれません、私に教えてください! –

2

これがに(

import 'rxjs/add/operator/first'; 
    .... 

    constructor(private router:Router, private routeSerializer:RouterUrlSerializer, private location:Location) { 
    router.changes.first().subscribe(() => { 

    let urlTree = this.routeSerializer.parse(location.path()); 
     console.log('id', urlTree.children(urlTree.children(urlTree.root)[0])[0].segment); 
    }); 
    } 

で行うことができます第2セグメントを得る)

MyComponentこれは簡単です:

関連する問題