简体   繁体   中英

Angular - Navigating to route with result of observable

I have an interesting issue that I can't seem to figure out.

I am making a HTTP request which is returning an Observable. I am subscribing to the observable and using the value as a parameter to my route when calling router.navigate. The navigation works correctly but when I land on the new route I can't click anything, the page is completely locked. As soon as I refresh the page everything works as expected.

Code snippet below for reference.

createTemplate() {
    this.templateService.create(this.templateModel)
        .subscribe(template => {
          this.router.navigate(['/admin/templates/builder', template.id]);
      });
    }

What this.templateService.create method does? What kind of Observable returns?

Maybe you have to close the subscription?

createTemplate() {
    this.templateService.create(this.templateModel)
        .pipe(first())
        .subscribe(template => {
          this.router.navigate(['/admin/templates/builder', template.id]);
      });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM