【问题标题】:data is not updating on page after routing to same url with different params路由到具有不同参数的相同网址后,页面上的数据未更新
【发布时间】:2019-09-06 12:01:26
【问题描述】:

基本上我有一个通知组件,它在点击标题中的图标时显示通知。当我们单击标题上的图标时,它会打开一个弹出窗口并显示通知列表。单击任何通知时,它将用户路由到该特定通知的组件,例如单击通知时,它将用户带到该路由:profile/guest/{guestid}/alerts/{alertid}。但是当我们点击上述路由的另一个通知时,它会更改路由参数,但不会重新加载新警报的数据,而只会显示旧路由的数据。

注意:显示的数据是从路由解析中获取的。并且由于组件已经加载,当我们点击另一个通知时,它不会调用 ngOnInit 或构造函数。但是当我们刷新页面时,它会根据更新后的路线显示正确的数据。

我尝试实施不同的路由器配置解决方案来重新加载解析数据。例如 runGuardsAndResolvers 和 onSameUrlNavigation。

我还尝试在其他 Angular 组件生命周期钩子(例如 ngAfterViewInit 或 ngAfterViewChecked)中调用设置数据的函数。

我尝试了更多的解决方案,但都没有用。

 Notification.component.ts :-(inside header component in shared module)
/**
   * * Callback when user clicks on visit button in notification modal
   * @param notification : notification 
   */
  navigateToGuestOrCustomer(notification: notification) {
    let routePath =
      notification.model == ALERT_MODEL.GUEST ? "guest" : "customers";
    let routeParamId = notification.detail_id;
    let alertId = notification.id;
    this.router.navigate([`/profile/${routePath}/${routeParamId}/alerts/${alertId}`]);
  }


edit-alert.component.ts(inside profile module in guest component>alert component)

  ngOnInit() {
    this.fetchRouteData();
    this.setGuestId();
    this.setGuestName();
  }

  /**
   * * get guest name from route resolve
   */
  setGuestName(): void {
    let guestData = this.route.parent.snapshot.data["editGuestInfo"];
    this.guestName = guestData["name"];
  }

  formData: any;
  alertListForGuest = [];
  /**Fetch Data from Resolve */
  fetchRouteData() {
    this.formData = this.route.snapshot.data["alertDetailForGuest"];
    this.alertListForGuest = this.route.snapshot.data["alertListForGuest"];
    this.alertListForGuest.push(this.formData.alert);
  }

预期结果:点击其他通知后,路由参数应该会发生变化,并显示更新数据的所需页面。

实际结果:只有路线发生变化,数据没有变化。

【问题讨论】:

    标签: angular routes router angular-resolver


    【解决方案1】:

    ngOnInit 可能不会被再次调用,如果只是路由改变,但组件没有重新创建。 您应该订阅路由更改并在那里执行您的获取和更新逻辑

    import { ActivatedRoute } from '@angular/router`
    constructor(private activatedRoute : ActivatedRoute){  }
    
    ngOnInit() {
        this.activatedRoute.url.subscribe(url =>{
            // Code to get the new notification data 
            // and display it
        });
    }
    

    这样当你导航到一个新的url时,代码会再次执行

    【讨论】:

      【解决方案2】:

      正如你所说,它在页面刷新后显示新数据,所以在这种情况下,只需尝试在同一页面上实现Route Reload,以便它重新加载当前路由并获取新数据。

      【讨论】:

        【解决方案3】:

        不知道为什么没有人提到ActivatedRoute.data,因为that's exactly what it's for

        此路由的静态数据和已解析数据的 observable。

        所以我通常会这样做:

        ngOnInit()
        {
            this.activatedRoute.data.pipe(takeUntil(this.destroyed)).subscribe(data =>
            {
                // data is your resolved data 
                this.model.page = data.page;
                this.model.options = data.options;
            });
        }
        

        此代码将针对初始数据和更新数据运行,因此您可以删除您已有的使用 activatedRoute.snapshot.data 的任何内容。

        其中this.destroyed 是一个Subject<void>,它在this.destroyed.next() 内完成ngOnDestroy()

        【讨论】:

        • 还有 paramsqueryParamsurl 的 observables。
        猜你喜欢
        • 2020-08-05
        • 1970-01-01
        • 2019-10-26
        • 2018-01-07
        • 2016-09-21
        • 2023-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多