简体   繁体   English

刷新Angular 4中的父组件视图

[英]Refresh the parent component view in Angular 4

I have got 2 components, let's say, Component A is a list view and Component B is a details view. 我有2个组件,比方说, Component A是一个列表视图,而Component B是一个详细视图。 Each row from the list view is clickable and will redirect to Component B upon clicking. 列表视图中的每一行都是可单击的,单击后将重定向到Component B

Component B allows editing and saving the details. Component B允许编辑和保存详细信息。 I have added a Back button to Component B to allow me to go back to the list view. 我在Component B添加了“ Back按钮,以使我可以返回列表视图。

But the problem I am having is that I can't see the updated list view and have to manually refresh the browser, and then I can see the updated list there. 但是我遇到的问题是我看不到更新的列表视图,而必须手动刷新浏览器,然后才能在此处看到更新的列表。

I have tried directly using window.location and it works but really I don't prefer this approach. 我已经尝试过直接使用window.location ,它可以工作,但实际上我不喜欢这种方法。

public back() {
   window.location.assign('/listview');
}

I wonder if there's any better way to solve this problem? 我想知道是否有更好的方法来解决这个问题?

Update: 更新:

public onSelected(model: MyModel) {
    const detailsViewUrl = `/detailsview/${model.id}`;
    this._router.navigateByUrl(detailsViewUrl );
  }

It sounds like this is an issue with Angular's change detection when changing the contents of an array. 听起来这是更改数组内容时Angular的更改检测问题。 See here: 看这里:

Angular 2: How to detect changes in an array? 角度2:如何检测阵列中的变化? (@input property) (@input属性)

The solutions in this questions should work but an easy way I have used in the past to force changes in an array to be recognised by Angular is to reassign the array after making the changes: 这个问题中的解决方案应该可以工作,但是我过去用来迫使Angular识别数组中的更改的一种简单方法是在进行更改后重新分配数组:

myArray = [...myArray];

use following routing fuction on back button click 在后退按钮上使用以下路由功能单击

public back() {
   this._router.navigateByUrl('/listview')
}
or
public back() {
   this._router.navigate('/listview')
}

Try this, 尝试这个,

Just called the list view again internally and hit db at same time so updated values will be displayed in the list view. 只是在内部再次调用了列表视图并同时命中db,所以更新的值将显示在列表视图中。

calling the route by using below: 使用以下命令调用路线:

this.router.navigate(['/listview']);

Seems like a change detection issue, there are some ways to manually trigger change detection like so: 似乎是一个更改检测问题,有一些方法可以手动触发更改检测,如下所示:

  1. Inject ChangeDetectorRef . 注入ChangeDetectorRef

  2. Call it when you go back like so: 当您返回时调用它,如下所示:

     public back() { ChangeDetectorRef.detectChanges() } 

Refer to this: Triggering change detection manually in Angular 参见: 在Angular中手动触发变更检测

You can just emit an @Output EventEmitter with a method on Parent that looks in the event for a change with a variable stored in the component like this: 您可以使用在Parent上的方法发出@Output EventEmitter,该方法在事件中使用存储在组件中的变量来查找更改,如下所示:

@Output someOutput: EventEmitter = new Event Emitter<any>;

HTML: HTML:

  <b-component (someOutput)=getOutput($event)></b-component>

AComponent: AComponent:

 getOut(event){
     let output = event;
     if(this.something != output){
          this.ngOnDestroy(); // or something that you can use to make it
    }

That should work as intended. 那应该按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM