【问题标题】:Angular2, How to reload a component only once?Angular2,如何只重新加载一次组件?
【发布时间】:2017-06-10 08:07:58
【问题描述】:

我有两个组件component Acomponent B

在组件 A 中,我向服务器发布了一个 HTTP 请求,并获得了 status="success" 的响应。在success 上,我使用queryParams of status='success' 导航到组件B。

在组件 B 中,如果我将 queryParam's 值设为“成功”,我将检查构造函数(),然后我只想重新加载一次并重定向到下一个组件 C。

我使用了location.reload(),但我最终一次又一次地重新加载组件 B。

有什么解决办法吗?

Component A
.subscribe(result => {
    if(result.status == 'success'){
        this.router.navigate(['/componentB'],{queryParams:{response: result.status}});
                                         }

component B    constructor(){
   this._activatedRoute.queryParams.subscribe(
       (queryParam: any) => this.response = queryParam['response']);
     if(this.response == 'success'){
            this.route.navigate(['/componentC']);
            window.location.reload();
   }

}

【问题讨论】:

  • 组件B的作用是什么?你有什么代码可以看吗?
  • 这就是组件 B 的全部功能吗?如果是这样,如果“成功”,您不能直接导航到 C,从我看来,B 是不必要的步骤。
  • 但由于某种原因,我曾经重新加载组件 B 并获取 result.status 表单参数。
  • window.location.reload(); 如果您使用延迟加载加载组件,则确实不需要

标签: angular angular2-routing angular2-template angular2-services angular2-directives


【解决方案1】:

导航到组件 C,然后导航到要重新加载的组件。 //第二个参数_skipLocationChange=true避免返回按钮中的url

this.route.navigateByUrl('/componentC', true);

【讨论】:

    猜你喜欢
    • 2017-02-20
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2017-09-09
    相关资源
    最近更新 更多