当我将可注入的 CameraChannelService 添加到 ngModule 的提供程序数组中时:
import { CameraChannelService } from './camera-channel.service';
@NgModule({
declarations: [
AppComponent,
BabylonWallpaperDirective,
MenuComponent,
WorksComponent,
LabsComponent,
ProfileComponent,
ActionBtnComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
routing
],
providers: [
CameraChannelService
],
bootstrap: [AppComponent]
})
它是否只为该组件创建一个新实例?当我将它注入到 NgModules 声明组件之一中时?
import { Component, OnInit } from '@angular/core';
import {CameraChannelService} from '../camera-channel.service';
@Component({
selector: 'app-works',
templateUrl: './works.component.html',
styleUrls: ['./works.component.scss'],
providers: [CameraChannelService]
})
export class WorksComponent implements OnInit {
constructor(private cameraChannel: CameraChannelService) { }
ngOnInit() {
console.log(this.cameraChannel);
}
}