【问题标题】:RxJs Router navigate not work from subscribeRxJs 路由器导航在订阅中不起作用
【发布时间】:2021-04-22 02:13:46
【问题描述】:

我使用 angular 10,并且有下一个代码

    this.recognizer.voiceText.subscribe((text) => {
  console.log(text)
  if (text.includes('menu')) {
    this.preProcess();
    this.opened = true;
    this.postProcess();
  }
  if (text.includes('bots')) {
    this.preProcess();
    this.router.navigate(['bots']);
    this.postProcess();
  }
}, error => {

}, () => {
});

所以我在“this.router.navigate”之后得到了奇怪的逻辑——Angular 只是在当前页面之后添加我的新页面。 请看1张现在的样子和以前的样子(2张)

【问题讨论】:

  • 请提供您的模板html 和您的...routing.module.ts 文件。或提供堆栈闪电战(例如)
  • @DavidB。在我的 html 中,我只有 =
    = 所以如果我打电话给路由器。按按钮导航 - 工作正常我不知道为什么订阅失败
  • 这在我看来像是嵌套 的问题,并且没有正确定义路由模块中的子路由。 (如 app.component.html 中的一个路由器插座和 menu.component.html 中的另一个)
  • @DavidB。我不这么认为。因为它工作得很好,如果我只是在没有 .subscribe() 的情况下调用 router.navigate
  • 您还能提供更多代码吗?例如。一个有效的。

标签: angular typescript rxjs router angular10


【解决方案1】:

我只是在构造函数中添加

private zone: NgZone

现在我这样使用它:

      this.zone.run(() => {
        this.router.navigate(['bots']);
      });

【讨论】:

    猜你喜欢
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 2017-04-06
    • 2016-04-24
    • 1970-01-01
    • 2018-07-04
    相关资源
    最近更新 更多