angular 全局监听变量


store.js


import {Injectable, Input} from '@angular/core';
import { BehaviorSubject } from 'rxjs';



@Injectable({
providedIn: 'root'
})
export class Store {
// 游戏大种类id
public game_type: BehaviorSubject<any[]> = new BehaviorSubject([]);

constructor(
) {
}
}


别的文件,修改变量的地方 this.store.game_type.next(data['game_type']);

 

监听变量更改的地方

public sub_game_type:Subscription;
this.sub_game_type = this.store.game_type.subscribe(data => {
if(data) {
}
})


取消监听 ngOnDestroy() { this.sub_game_type.unsubscribe(); }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM