admin管理员组文章数量:1244384
To access ponents in the current URL path, the following works in my application:
constructor(private route: ActivatedRoute) { }
...
load() {
this.route.params.subscribe((params) => {
const id = +params['id'];
console.log('got parameter', id);
... etc
This is the textbook case and the variable id gets set as advertised. But I want to use the more elegant await form and it doesn't work. (Of course I will have a string of await statements following.) Like this:
async load() {
try {
console.log('getting parameters');
const params = await this.route.params.toPromise();
console.log('got params', params);
const id = +params['id'];
... etc
What I get is the output from the first console.log() call but the second never es. And of course the id variable doesn't get set and the rest of the code never executes.
The documentation and the working code says that this.route.params is an Observable, which should be convertable to a Promise via toPromise() and thereby used with Typescript async/await. I use await statements with the Observables that e out of the HttpClient module just fine.
But it doesn't work. Anyone know why?
To access ponents in the current URL path, the following works in my application:
constructor(private route: ActivatedRoute) { }
...
load() {
this.route.params.subscribe((params) => {
const id = +params['id'];
console.log('got parameter', id);
... etc
This is the textbook case and the variable id gets set as advertised. But I want to use the more elegant await form and it doesn't work. (Of course I will have a string of await statements following.) Like this:
async load() {
try {
console.log('getting parameters');
const params = await this.route.params.toPromise();
console.log('got params', params);
const id = +params['id'];
... etc
What I get is the output from the first console.log() call but the second never es. And of course the id variable doesn't get set and the rest of the code never executes.
The documentation and the working code says that this.route.params is an Observable, which should be convertable to a Promise via toPromise() and thereby used with Typescript async/await. I use await statements with the Observables that e out of the HttpClient module just fine.
But it doesn't work. Anyone know why?
Share Improve this question asked Aug 1, 2018 at 4:42 AlanObjectAlanObject 9,96319 gold badges93 silver badges154 bronze badges2 Answers
Reset to default 13The basic answer is: You are having incontrollable race conditions here
When you access the Observable route.params
it's pretty normal that it isn't pleted yet. Calling toPromise()
on it at this point in time will lead to an enduringly pending Promise and this is the problem you are facing.
If you are not bound to using this async await construct leave it and better directly access synchronously params.id
via ActivatedRouteSnapshot.
ngOnInit() {
const id: string = route.snapshot.params.id;
}
Apropos AlanObject's question ... why the ActivatedRoute uses Observables in the first place ..., the reason is the ngOnInit()
is called only on ponent initiation, that is, when it is created. Imagine your ponent is used to display item details with a route like /item/123
, where 123
is the current item ID
.
Now, if your ponent would implement also a Previous and Next links navigating to /item/122
and /item/124
, the ponent would not be instantiated again. In such cases you would need a subscription or another way to get the ID
and related data updated.
本文标签: javascriptAngular promise from ActivatedRoute doesn39t work with Typescript awaitStack Overflow
版权声明:本文标题:javascript - Angular promise from ActivatedRoute doesn't work with Typescript await - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740152225a2232874.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论