AngularElements-默认情况下多个实例共享相同的服务
创始人
2024-10-24 17:00:59
0
  1. 创建一个共享的服务,它负责处理需要共享的逻辑。
  2. 在module中将该服务提供给root,以确保整个应用程序中只有一个实例。
  3. 使用该服务来将数据或状态共享给任意数量的Angular Elements实例。

示例代码: // shared service import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' }) export class SharedDataService { data: any; }

// angular element component import { Component } from '@angular/core'; import { SharedDataService } from './shared-data.service';

@Component({ selector: 'my-angular-element', template: '

{{data}}
' }) export class MyAngularElement { data: any;

constructor(private sharedDataService: SharedDataService) { this.data = sharedDataService.data; } }

// app component import { Component } from '@angular/core'; import { SharedDataService } from './shared-data.service';

@Component({ selector: 'my-app', template: }) export class AppComponent { constructor(private sharedDataService: SharedDataService) { this.sharedDataService.data = 'Hello World'; } }

这里,我们创建了一个名为SharedDataService的服务,它在整个应用程序中都是提供的。我们在Angular Elements的构造函数中注入该服务,并使用它来共享数据。在AppComponent中,我们将SharedDataService的data属性设置为“Hello World”,这意味着两个Angular Elements实例将共享相同的数据。

相关内容

热门资讯

辅助开挂!多乐游戏脚本,wep... 辅助开挂!多乐游戏脚本,wepoker线上大神(透视)开挂辅助神器(有挂秘籍);1.多乐游戏脚本 a...
总算了解“拱趴大菠萝万能挂图解... 您好:这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,总是好牌...
辅助开挂!湖北逍遥卡五星辅助,... 辅助开挂!湖北逍遥卡五星辅助,哈糖大菠萝助手(透视)开挂辅助神器(有挂辅助);小薇(透视辅助)致您一...
研究成果“we-poker是什... 研究成果“we-poker是什么软件”开挂(透视)辅助平台(安装教程有挂方法);无需打开直接搜索微信...
开挂辅助!广东雀神挂件骗局,w... 开挂辅助!广东雀神挂件骗局,wepokerplus辅助(透视)开挂辅助插件(有挂教程);广东雀神挂件...
玩家必看分享“wepoker透... 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、软件助手是一款功...
辅助开挂!欢乐对决辅助软件,w... 辅助开挂!欢乐对决辅助软件,wpk透视辅助方法(透视)开挂辅助下载(有挂方法);AI辅助机器人普及解...
查到实测“hhpoker辅助挂... 查到实测“hhpoker辅助挂是真的吗”开挂(透视)辅助软件(透牌教程新版有挂);无需打开直接搜索打...
辅助开挂!贵阳辅助工具,wep... 辅助开挂!贵阳辅助工具,wepoker免费脚本咨询(透视)开挂辅助软件(有挂工具);相信小伙伴都知道...
玩家必备攻略“约局吧德州有挂吗... 玩家必备攻略“约局吧德州有挂吗”开挂(透视)辅助平台(AI教程有挂实锤)《详细加薇136704302...