是的,我们可以在Angular自定义库中导出多个服务。我们只需要在我们的自定义库中创建多个服务文件,并在导出时将它们包含在内即可。
举个例子,我们在我们的自定义库中有两个服务:foo.service.ts 和 bar.service.ts。为了在我们的库中导出这两个服务,我们要执行以下步骤:
创建 foo.service.ts 和 bar.service.ts 两个服务文件。
在这两个服务文件中实现我们需要的服务逻辑。在这个例子中,我们在 foo.service.ts 中实现了 FooService,在 bar.service.ts 中实现了 BarService。
FooService
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class FooService {
constructor() { }
doSomething(): void {
console.log('FooService.doSomething()');
}
}
BarService
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class BarService {
constructor() { }
doSomething(): void {
console.log('BarService.doSomething()');
}
}
import { FooService } from './foo.service';
import { BarService } from './bar.service';
export * from './foo.service';
export * from './bar.service';
export const services = [FooService, BarService];
import { Component, OnInit } from '@angular/core';
import { FooService, BarService } from 'my-custom-library';
@Component({
selector: 'app-root',
template: `
`
})
export class AppComponent implements OnInit {
constructor(public foo: FooService, public bar: BarService) { }
ngOnInit(): void { }
}