angular订阅成功后如何导航

问题描述 投票:0回答:1

Angular 8。当用户更新模型时,我希望他们被重定向到一个叫做 dashboard 的页面,这样他们就可以看到新的值。我目前的做法是,一旦用户被重定向到了一个新的页面,就可以看到新的值。dashboard 页面,你必须刷新浏览器才能看到更新。我想我可以在 subscribe() 方法,但我似乎想不通... ...

  constructor(
    private restaurantservice: RestaurantService,
    private router: Router
  ) { }

this.restaurantservice.restaurantedit(id, formData).subscribe({
  complete(){
    this.router.navigate(['/ownerdashboard'])
  }
})

}

我一直得到 ERROR TypeError: Cannot read property 'navigate' of undefined.

angular subscribe
1个回答
1
投票

你需要使用箭头功能,否则 this 将上下文更改为您所定义的对象 complete 方法。

  constructor(
    private restaurantservice: RestaurantService,
    private router: Router
  ) { }

this.restaurantservice.restaurantedit(id, formData).subscribe({
  complete: () => { // here you need to use arrow function
    this.router.navigate(['/ownerdashboard'])
  }
})

1
投票
.subscribe(value => console.log(value),
           error => console.log(error),
           () => this.router.navigate(['/ownerdashboard'])
    )

试试这个。

© www.soinside.com 2019 - 2024. All rights reserved.