Angular库中的服务与本地服务不同,如何解决?(需要代码示例)
创始人
2024-10-27 21:32:23
0

Angular库中的服务和本地服务不同,主要是因为Angular库的服务是单例模式,而本地服务是每次都会创建一个新实例。为了解决这个问题,需要在Angular库中使用Injector创建每个服务的实例。

首先,需要在Angular库的服务提供商中将其标记为可被多重实例化,如下所示:

@Injectable({ providedIn: 'any' }) export class MyService { }

然后,在使用服务的地方,可以使用Injector创建新的实例:

import { Component, Injector } from '@angular/core'; import { MyService } from 'my-library';

@Component({ selector: 'my-component', template: }) export class MyComponent { private myService: MyService; constructor(private injector: Injector) { }

createService() { this.myService = this.injector.get(MyService); }

useService() { this.myService.doSomething(); } }

这样,每次点击“Create Service”按钮时,都会创建一个新的MyService实例。

相关内容

热门资讯

透视教学"pokem... 这是一款非常优秀的pokemmo辅助器手机版下载 ia辅助检测软件,能够让你了解到pokemmo辅助...
透视玄学"wepok... 透视玄学"wepoker免费脚本弱密码"详细辅助我来教教你(科技辅助器技巧)准备好在wepoker免...
透视智能ai"hhp... 透视智能ai"hhpoker到底可以作弊吗"详细辅助AI教程(黑侠辅助器透明);hhpoker到底可...
透视新版"wepok... 透视新版"wepoker免费辅助器"详细辅助科技教程(可以透视码辅助)1、很好的工具软件,可以解锁游...
透视教学"wepok... 1、透视教学"wepoker透视脚本"详细辅助2025版教程(透视破解版透明);详细教程。2、wep...
透视数据"hhpok... 透视数据"hhpoker必备开挂"详细辅助细节方法(透视脚本安卓方法)关于hhpoker必备开挂机制...
透视安装"wepok... 透视安装"wepoker怎么挂底牌"详细辅助AI教程(轻量版透视透视);致您一封信;亲爱wepoke...
透视ai"智星德州插... 透视ai"智星德州插件怎么下载"详细辅助2025新版教程(挂辅助)准备好在智星德州插件怎么下载 ia...
透视透视"wepok... 1、透视透视"wepoker辅助透视软件"详细辅助力荐教程(免费脚本神器);详细教程。2、wepok...
透视计算"哈糖大菠萝... 透视计算"哈糖大菠萝破解器"详细辅助解密教程(正规技巧);亲真的是有正版授权,小编(透视辅助软件透明...