Angular 8。当用户更新模型时,我希望仅在数据库更新后将他们重定向回仪表板
页面,以便他们可以看到新值。我目前的方式是,一旦用户重定向到仪表板页面,您就必须刷新浏览器才能看到更新。我以为我可以在 subscribe()
方法中执行此操作,但我似乎无法弄清楚..
constructor(
private restaurantservice: RestaurantService,
private router: Router
) { }
this.restaurantservice.restaurantedit(id, formData).subscribe({
complete(){
this.router.navigate(['/ownerdashboard'])
}
})
}
我不断收到错误类型错误:无法读取未定义的属性“导航”
。
最佳答案
您需要使用箭头函数,否则 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'])
}
})
关于angular - Angular 订阅成功后如何导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61754389/