无法获取参数路径角度为6 [英] cant get the param route angular 6
问题描述
我正试图从url中获取[param],并获取一些信息以该param抛出一个服务,然后(仅此)转到另一个页面.
I'm trying to get the [param] from url, and get some information throw a service with this param, and then (ONLY then) go to another page.
Chome网址: http://localhost:4200/login/asd
appmodule:
appmodule:
{ path: 'login/:token', component: AppComponent},
appcomponent.ts
appcomponent.ts
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.usr = params['token'];
console.log(this.usr); //THIS KEEP GETTING UNDEFINED
});
}
该usr一直处于未定义状态,不知道为什么... 有什么主意吗?谢谢.
That usr keeps getting undefined, dont know why... Some idea? Thanks.
我尝试过这样的事情:
this.sub = this.route.params.subscribe(params => {
if (!params['token']) {
console.log('NO token'); //GOES HERE EVERYTIME
}
this.token = params['token'];
console.log(this.token);
});
每次都以"NO令牌"发出. 网址有问题吗?
It gets out in 'NO token' every single time.. Something wrong with the url maybe?
推荐答案
打开了另一个描述您的问题的问题.这与a router-outlet
之外的ActivatedRoute
状态有关,在您的情况下,我认为您没有此状态.
There is another question opened that describes your issue. This is related to ActivatedRoute
state outside of arouter-outlet
, in your case I believe you don't have it.
我在stackbliz中进行了一个测试,您可以看到它与router-outlet
一起工作,但是要做到这一点,我不得不将路由器路径更改为另一个组件(AppComponent
是根组件,因此我没有使用它).
I did a test in stackbliz where you can see it working with router-outlet
, but to do this I had to change the router path to another component(The AppComponent
is the root component, so I did not use it).
这是stackblitz示例:
Here is the stackblitz example:
https://stackblitz.com/edit/angular-izk361
有关此问题的更多信息.
And here much more info about this problem.
在服务中或< router-outlet>外部不可用的Angular 2激活的路由参数
这篇关于无法获取参数路径角度为6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!