在Angular 6上设置自动刷新页面

问题描述 投票:-1回答:1

我的角度上有3个不同的页面(我使用Angular 6),但我仍然混淆如何使我的所有页面自动刷新/重新加载间隔时间。

有没有办法让功能等?与每个页面component.ts,component.html或app-routing.module中的自动刷新/重新加载有关?

有什么建议或经验可以做出类似的事情吗?

例如component.ts:

import { Component, OnInit } from '@angular/core';

@Component({
 selector: 'app-example-page',
 templateUrl: './example-page.component.html',
 styleUrls: ['./example-page.component.less']
})
export class ExamplePageComponent implements OnInit {

  constructor() { }

  ngOnInit() {

  }

}
angular
1个回答
1
投票

import { Observable, interval, Subscription } from 'rxjs';

export class YourComponent ... {

  private updateSubscription: Subscription;

  ngOnInit() {
      this.updateSubscription = interval(1000).subscribe(
        (val) => { this.updateStats()
      }
  );

  ngOnDestroy() {
      this.updateSubscription.unsubscribe();
  }

  private updateStats() {
      console.log('I am doing something every second');
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.