在Angular应用程序的提供者中注册所需的服务,以使其可在应用程序中的任何组件中使用。 示例代码:
import { Injectable } from '@angular/core'; import { AnotherService } from './another.service';
@Injectable() export class MyService { constructor(private anotherService: AnotherService) {} }
@NgModule({ providers: [AnotherService, MyService], }) export class AppModule {}