Angular 2 RC1:从使用的初始URL获取参数
一些用户通过邀请进入我的web应用程序。所以他们会有一个类似这样的链接:Angular 2 RC1:从使用的初始URL获取参数,angular,Angular,一些用户通过邀请进入我的web应用程序。所以他们会有一个类似这样的链接:https://example.com/invitaion/12345其中12345是他们的唯一邀请编号 当用户单击链接,并且框架在客户端初始化了我的AppComponent时,如何获得所使用的URL为“invitation/*”的事实,以及如何获得邀请号 您需要使用RouteParams访问该变量,并在组件中使用它 // let's assume you labeled it as `path : '/invitation
https://example.com/invitaion/12345
其中12345是他们的唯一邀请编号
当用户单击链接,并且框架在客户端初始化了我的AppComponent时,如何获得所使用的URL为“invitation/*”的事实,以及如何获得邀请号 您需要使用
RouteParams
访问该变量,并在组件中使用它
// 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');
}
}
在新路由器(=RC.0)的AppComponent
中,这可以通过
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);
});
}
(获取第二段)
在MyComponent
中,这更容易:
routerOnActivate(curr:RouteSegment, prev?:RouteSegment, currTree?:RouteTree, prevTree?:RouteTree):void {
this.id = curr.getParam('id');
}
您可能需要尝试从
@angular/router deprecated
导入RouteParams,如果这不起作用,请告诉我!我知道这是一个旧的响应,但导入对我不起作用:模块../../../../../../../../../../../../node\u modules/@angular/router/router“'没有导出的成员“RouteParams”。ts(2305)
与@angular/router不推荐的相同