【问题标题】:Angular 8 - navigate to a component then refresh windowAngular 8 - 导航到一个组件然后刷新窗口
【发布时间】:2020-11-30 11:53:57
【问题描述】:

我想导航到一个组件,然后在单击 switch() 按钮时刷新窗口。

我试过了:

switch() {
    this.router.navigateByUrl("/layouts");
    window.location.reload();
  }

但它没有按预期工作,它只是重新加载页面而不导航。

【问题讨论】:

    标签: angular routes state angular8


    【解决方案1】:

    navigateByUrl 是异步的,因此 window.location.reload() 在返回之前被调用,导致页面在导航之前重新加载。

    navigateByUrl 返回一个承诺,所以你可以这样做:

    this.router.navigateByUrl("/layouts").then(() => {
        window.location.reload();
    });
    

    【讨论】:

    • 我建议添加它没有按 OP 预期工作的原因。
    猜你喜欢
    • 2020-05-08
    • 2011-04-28
    • 1970-01-01
    • 2018-05-31
    • 2018-05-31
    • 2016-11-02
    • 2020-05-26
    • 2016-11-18
    • 1970-01-01
    相关资源
    最近更新 更多