Angular - 单例服务只传递给一个组件的更改
创始人
2024-10-14 15:00:55
0

在Angular中,单例服务是指在整个应用程序中只有一个实例的服务。如果您想将单例服务的更改传递给一个组件,可以使用事件或RxJS的Subject来实现。

以下是使用事件的示例:

  1. 创建一个单例服务:
import { Injectable, EventEmitter } from '@angular/core';

@Injectable()
export class MySingletonService {
  public dataChanged: EventEmitter = new EventEmitter();
  
  private data: any;

  setData(data: any) {
    this.data = data;
    this.dataChanged.emit(this.data);
  }
}
  1. 在要接收更改的组件中订阅事件:
import { Component, OnInit } from '@angular/core';
import { MySingletonService } from './my-singleton.service';

@Component({
  selector: 'app-my-component',
  template: `
    
{{ data }}
` }) export class MyComponent implements OnInit { data: any; constructor(private singletonService: MySingletonService) { } ngOnInit() { this.singletonService.dataChanged.subscribe((data: any) => { this.data = data; }); } }
  1. 在发送更改的组件中更新数据:
import { Component } from '@angular/core';
import { MySingletonService } from './my-singleton.service';

@Component({
  selector: 'app-another-component',
  template: `
    
  `
})
export class AnotherComponent {
  constructor(private singletonService: MySingletonService) { }

  updateData() {
    const newData = 'New data';
    this.singletonService.setData(newData);
  }
}

使用RxJS的Subject的示例:

  1. 创建一个单例服务:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable()
export class MySingletonService {
  public dataChanged: Subject = new Subject();
  
  private data: any;

  setData(data: any) {
    this.data = data;
    this.dataChanged.next(this.data);
  }
}
  1. 在要接收更改的组件中订阅Subject:
import { Component, OnInit } from '@angular/core';
import { MySingletonService } from './my-singleton.service';

@Component({
  selector: 'app-my-component',
  template: `
    
{{ data }}
` }) export class MyComponent implements OnInit { data: any; constructor(private singletonService: MySingletonService) { } ngOnInit() { this.singletonService.dataChanged.subscribe((data: any) => { this.data = data; }); } }
  1. 在发送更改的组件中更新数据:
import { Component } from '@angular/core';
import { MySingletonService } from './my-singleton.service';

@Component({
  selector: 'app-another-component',
  template: `
    
  `
})
export class AnotherComponent {
  constructor(private singletonService: MySingletonService) { }

  updateData() {
    const newData = 'New data';
    this.singletonService.setData(newData);
  }
}

无论您选择使用事件还是RxJS的Subject,都可以将单例服务的更改传递给一个组件。

相关内容

热门资讯

第十分钟妙招!aapoker怎... 第十分钟妙招!aapoker怎么设置抽水(透视)都是真的是有辅助教程(哔哩哔哩)aapoker怎么设...
9分钟讲义!wepoker一直... 9分钟讲义!wepoker一直输的号能继续打吗(透视)其实存在有辅助插件(哔哩哔哩)1、玩家可以在w...
第一分钟大纲!wepoker辅... 第一分钟大纲!wepoker辅助器安装包定制(透视)竟然真的是有辅助透视(哔哩哔哩)1、wepoke...
第三分钟机巧!德州局怎么透视(... 第三分钟机巧!德州局怎么透视(透视)本来是真的有辅助插件(哔哩哔哩)1.德州局怎么透视 选牌创建新账...
第二分钟方针!pokernow... 第二分钟方针!pokernow辅助工具(透视)真是真的有辅助神器(哔哩哔哩)所有人都在同一条线上,像...
第1分钟窍要!we-poker... 第1分钟窍要!we-poker是什么软件(透视)真是有辅助神器(哔哩哔哩)1、全新机制【we-pok...
第1分钟阶段!wepoker科... 第1分钟阶段!wepoker科技辅助器(透视)一直有辅助安装(哔哩哔哩)1、超多福利:超高返利,海量...
3分钟总结!hhpoker可以... 3分钟总结!hhpoker可以控制牌吗(透视)本来是真的有辅助工具(哔哩哔哩)1、任何hhpoker...
四分钟项目!wpk模拟器(透视... 四分钟项目!wpk模拟器(透视)切实是真的有辅助插件(哔哩哔哩)亲,关键说明,wpk模拟器透视脚本安...
十分钟举措!aa poker辅... 十分钟举措!aa poker辅助(透视)都是是有辅助插件(哔哩哔哩)1、aa poker辅助公共底牌...