【问题标题】:this.navigate.url not working in promisethis.navigate.url 不能按承诺工作
【发布时间】:2017-10-26 22:58:54
【问题描述】:

我想在执行承诺时刷新页面(通过 this.navigate.url),但我似乎无法使其工作。

import {Router} from '@angular/router';

constructor(public router: Router) {
}

myPromise('/api/users').then((response) => {
    return response.json();
}).then(function (response) {
    this.router.navigate.url([url]);
});

我什至尝试过 window.location.reload,但它也不起作用。

【问题讨论】:

  • 这里的this 是什么? this.navigate 是做什么的? url 来自哪里?控制台显示什么?
  • 您想在 Angular 路由器 och 的帮助下在您的应用程序内部导航到外部站点吗?如果是外部的,请尝试window.location.replace("http://something.com");
  • @torazaburo 我编辑了我的问题以进一步阐述。 router 是Router 类的一个实例,url 是当前页面的URL。我想要的是在执行承诺后刷新当前页面。
  • @FredrikLundin 我想在成功执行承诺后刷新我的页面。
  • @torazaburo 是否有参考来纠正打字稿/角度的这个答案?喜欢:stackoverflow.com/questions/20279484/…

标签: angular angular2-routing


【解决方案1】:
constructor(private zone:NgZone) {}

this.zone.run(() => this.router.navigate(['main']));



//import NgZone from angular core, use zone.run inside the promise 

【讨论】:

  • 欢迎来到 StackOverflow。一些解释将大大提高答案的质量,并帮助以后可能遇到类似问题的其他用户请花一些时间阅读帮助页面stackoverflow.com/help/how-to-answer
【解决方案2】:

Angular 6.0 路由器链接始终在子组件中禁用。

如果我们的导航代码在 Promise 中,子组件中的路由器链接总是被禁用。为了解决这个问题,我们可以在 Promise 中使用 NgZone。

让我给你看一下文件内容:

login.component.ts

import { NgZone } from '@angular/core'; //import ngZone from angular core

constructor(private zone:NgZone) {}

authorize(): Promise<any> {
return new Promise((resolve, reject) => {
  let promise = this.auth2.signIn();
  promise.then(() => {
    let self = this;
    let profile = this.auth2.currentUser.get().getBasicProfile();
    this.authenticationService.login(profile);
    this.zone.run(() => this.router.navigate(['/contacts']));
  });
});
}

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 2016-09-05
    • 1970-01-01
    • 2019-08-25
    • 2018-09-19
    • 2018-04-09
    相关资源
    最近更新 更多