Angular 2 RC1:从使用的初始URL获取参数 [英] Angular 2 RC1: Get parameters from the initial URL used

查看:71
本文介绍了Angular 2 RC1:从使用的初始URL获取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些用户通过邀请进入我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆