简体   繁体   English

如何刷新 angular 组件

[英]How To refresh an angular component

I'm building a mobile app using ionic 4 and it's two languages ar and en , the menu drawer is a pre-built component.我正在使用ionic 4构建一个移动应用程序,它有两种语言aren ,菜单抽屉是一个预先构建的组件。

So I need to refresh the drawer component to get's the right styles based on the dom direction rtl or ltr所以我需要刷新抽屉组件以根据 dom 方向rtlltr获得正确的 styles

What I'm doing now is just location.reload reloading the whole app but I don't think that a good approach for doing that我现在正在做的只是location.reload重新加载整个应用程序,但我认为这不是一个好方法

Simplest way to do is to call ngOnInit something like最简单的方法是调用 ngOnInit 类似

fn() {
    this.ngOnInit();
}

Or try this或者试试这个

this.router.navigateByUrl('/RefreshComponent', { skipLocationChange: true })
  .then(() => {
      this.router.navigate(['Your actualComponent']);
}); 

call this method whenever you want to reload your page.每当您想重新加载页面时调用此方法。
that will reload the page这将重新加载页面

 window.location.reload();

For Refreshing or re drawing the component use thechangeDetectionStratergy对于刷新或重新绘制组件,请使用changeDetectionStratergy

so inject the ChangeDetectionRef Service in the component where you using the build-in component so on any event call the detectChange method for redrawing the component.因此,在使用内置组件的组件中注入ChangeDetectionRef服务,以便任何事件调用detectChange方法来重绘组件。

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

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