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实例。

相关内容

热门资讯

透视科普"xpoke... 透视科普"xpoker辅助器"都是是有辅助工具(哔哩哔哩)1、玩家可以在xpoker辅助器透视最简单...
透视专业"wepok... 透视专业"wepoker辅助软件视频"一直是有辅助软件(哔哩哔哩)透视专业"wepoker辅助软件视...
透视了解"约局吧德州... 透视了解"约局吧德州真的存在透视吗"确实是有辅助挂(哔哩哔哩)1、实时约局吧德州真的存在透视吗透视辅...
透视教你"pokem... 透视教你"pokemmo脚本手机版"原来是真的辅助脚本(哔哩哔哩)1、用户打开应用后不用登录就可以直...
透视推荐"wepok... 透视推荐"wepoker插件下载"真是有辅助脚本(哔哩哔哩)运wepoker插件下载辅助工具,进入游...
透视解迷"wepok... 透视解迷"wepoker能不能透视"本来存在有辅助插件(哔哩哔哩)1、任何wepoker能不能透视透...
透视专业"wepok... 透视专业"wepoker私人辅助器"都是是有辅助app(哔哩哔哩);1、打开软件启动之后找到中间准星...
透视解迷"we-po... 透视解迷"we-poker辅助"总是有辅助攻略(哔哩哔哩)在进入we-poker辅助软件靠谱后,参与...
透视有挂"we-po... 透视有挂"we-poker辅助器"原来有辅助插件(哔哩哔哩)1、该软件可以轻松地帮助玩家将we-po...
透视必备"xpoke... 透视必备"xpoker辅助怎么用"其实真的有辅助攻略(哔哩哔哩)xpoker辅助怎么用透视方法中分为...