//global.service.ts

import { Injectable } from "@angular/core"
import { Subject } from "rxjs"
interface globalModalModel {
  tipMsg?: string
  show?: boolean
  duration?: number
}
@Injectable({
  providedIn: "root"
})
export class GlobalService {
  constructor() {}
  private globalModal = new Subject<any>()
  globalModal$ = this.globalModal.asObservable()
  updateGlobalModal(data: globalModalModel) {
    this.globalModal.next(data)
  }

  public sysTime
  public globalTime = new Subject<any>()
  globalTime$ = this.globalTime.asObservable()
  updateGlobalTime(data: any) {
    this.globalTime.next(data)
    this.sysTime = data
  }
}

监听数据变化:

 this.subscription_globalModal = this.GlobalService.globalModal$.subscribe(
      (data) => {
        this.tipMsg = data.tipMsg || ""
        this.globalMaskShow = data.show
        setTimeout(() => {
          this.globalMaskShow = false
        }, data.duration || 3000)
      }
    )

 

更新数据:

 this.GlobalService.updateGlobalModal({
            show: true,
            duration: 1000000,
            tipMsg: "服务器连接中断, 正在尝试重新连接....."
          })

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
  • 2022-12-23
  • 2021-10-05
  • 2022-02-10
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-21
  • 2022-12-23
  • 2021-08-02
  • 2021-10-02
  • 2022-12-23
  • 2021-08-21
相关资源
相似解决方案