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,都可以将单例服务的更改传递给一个组件。

相关内容

热门资讯

我来教教你!哈糖大菠萝万能挂,... 我来教教你!哈糖大菠萝万能挂,聚星ai辅助工具收费多少,实用技巧(有挂透明)关于聚星ai辅助工具收费...
玩家攻略推荐!wepoker有... 玩家攻略推荐!wepoker有辅助吗,德普之星透视辅助软件,2025版教程(有挂攻略);1.德普之星...
发现一款!werplan有挂吗... 发现一款!werplan有挂吗,wejoker私人辅助软件,必胜教程(有挂技巧);1.wejoker...
一分钟秒懂!wepoker安装... 一分钟秒懂!wepoker安装教程,来玩app 德州 辅助,教你攻略(有挂攻略)是由北京得来玩app...
玩家必看科普!智星德州可以透视... 玩家必看科普!智星德州可以透视吗,sohoo辅助,教你教程(有挂教程);人气非常高,ai更新快且高清...
玩家实测!wepoker轻量版... 玩家实测!wepoker轻量版有透视吗,来玩app破解版,wpk教程(有挂攻略);支持多人共享记分板...
分享个大家!wepoker有辅... 分享个大家!wepoker有辅助吗,德普辅助软件,安装教程(有挂方法);原来确实真的有挂(需添加指定...
一分钟秒懂!wepoker辅助... 一分钟秒懂!wepoker辅助器有哪些功能,wepoker免费脚本咨询,玩家教你(有挂攻略)准备好在...
玩家必看科普!wepoker轻... 玩家必看科普!wepoker轻量版透视方法,拱趴大菠萝自动计算机器人,新2025教程(有挂攻略)准备...
科普常识!佛手大菠萝有挂吗,w... 科普常识!佛手大菠萝有挂吗,wepoker辅助透视,分享教程(有挂透视);佛手大菠萝有挂吗软件透明挂...