angular - Angular 订阅成功后如何导航

标签 angular subscribe

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/

相关文章:

html - 浏览器放大/缩小与不同屏幕尺寸相同吗?

maven - 在 Maven 中构建 Angular 2 项目

amazon-web-services - 可以在 lambda 中订阅 aws IOT 主题吗?

Angular 订阅@Input 对象变化

youtube - 用户订阅和退订时如何获取Youtube API通知?

javascript - 我如何在客户端(即网络浏览器)找到 MAC 地址?

angular - 错误 : Cannot match any routes. URL 段: 'homePage'

subscribe - 提供的网络推送 applicationServerKey 无效

javascript - Angular react 形式内的切换按钮

压力测试下的Redis hooking(发布-订阅)——负载下的性能