Angular 2 RC1:从使用的初始URL获取参数 [英] Angular 2 RC1: Get parameters from the initial URL used
问题描述
某些用户通过邀请进入我的Web应用程序.因此他们会有一个看起来像这样的链接:https://example.com/invitaion/12345
其中12345是其唯一的邀请号码.
Some users enter my web app via invitations. so they would have a link that looks something like this: https://example.com/invitaion/12345
where 12345 is their unique invitation number.
当用户单击链接,并且框架在客户端初始化了我的AppComponent时,如何获得使用的URL为"invitation/*"的事实以及如何获得邀请号?
When users click the link, and my AppComponent is initialized on the client side by the framework, how do I get the fact that the URL used was "invitation/*" and how do I get the invitation number?
推荐答案
您将要使用RouteParams
访问该变量,并在您的组件中使用它.
You'll want to use RouteParams
to access that variable, and use it within your Component.
// let's assume you labeled it as `path : '/invitation/:id'` in your @Route setup.
@Route([
{ path : '/invitation/:id', component : MyComponent }
])
现在在组件本身中:
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');
}
}
这篇关于Angular 2 RC1:从使用的初始URL获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!