[英]Refreshing a component in Angular 6
我正在设计一个基于angular 6
的仪表板。 我的仪表板有几个不同的组件排列在一起。 我想每 5 分钟刷新一次组件。
我已经尝试过windows.location.reload()
和location.reload()
但是它正在刷新整个页面,而不仅仅是一个组件,也就是说,我的所有组件都在刷新。 所以,请提前帮助和感谢。
试试这个
constructor(private ref: ChangeDetectorRef) {
setInterval(() => {
this.ref.detectChanges();
}, 5000);
}
干得好,
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"]));
});
或者
import {Observable} from 'rxjs'; // Angular 6
// import {Observable} from 'rxjs/Rx'; // Angular 5
Observable.interval(1000).subscribe(x => {
this.ngOnInit();
});
以下对我有用
ngOnInit(): void {
setTimeout(() => { this.ngOnInit() }, 1000 * 10)
//Interval = 10secs
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.