[英]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.