简体   繁体   English

在 Angular 6 中刷新组件

[英]Refreshing a component in Angular 6

I am designing a dashboard based on angular 6 .我正在设计一个基于angular 6的仪表板。 My dashboard has several different components arranged together.我的仪表板有几个不同的组件排列在一起。 I want to refresh a component after every 5 minutes.我想每 5 分钟刷新一次组件。

I have tried windows.location.reload() and location.reload() however it is refreshing the entire page and not just one component , ie to be specefic all my components are getting refreshed.我已经尝试过windows.location.reload()location.reload()但是它正在刷新整个页面,而不仅仅是一个组件,也就是说,我的所有组件都在刷新。 So, please help and thanks in advance.所以,请提前帮助和感谢。

Try this out试试这个

constructor(private ref: ChangeDetectorRef) {    
setInterval(() => {
  this.ref.detectChanges();
 }, 5000);
}

Here you go,干得好,

import {Observable} from 'rxjs'; // Angular 6 
// import {Observable} from 'rxjs/Rx'; // Angular 5

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

OR或者

import {Observable} from 'rxjs'; // Angular 6 
// import {Observable} from 'rxjs/Rx'; // Angular 5

  Observable.interval(1000).subscribe(x => {
    this.ngOnInit();
  });

The following worked for me以下对我有用

ngOnInit(): void {
  setTimeout(() => { this.ngOnInit() }, 1000 * 10)
  //Interval = 10secs
}

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

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