为什么仅使用私有访问说明符来实例化构造函数中的提供程序?是否有任何特定原因仅使用私有访问说明符?
私人注射
constructor(private service: InjectedService)
Run Code Online (Sandbox Code Playgroud)
公众注射
constructor(service: InjectedService)
Run Code Online (Sandbox Code Playgroud) 在angular2 rc5版本中,在ngModule提供程序(根级别)中注入一个服务并尝试在组件中使用它,但是遇到了无法找到"servicename"的问题.这是我的代码,
根文件:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ApplicationRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { OtherService } from './other.service';
@NgModule({
declarations: [ AppComponent ],
imports: [ BrowserModule, CommonModule, FormsModule ],
providers: [OtherService],
entryComponents: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
服务:
import { Injectable } from '@angular/core';
@Injectable()
export class OtherService {
getUsername(){
return 'from other service';
} …Run Code Online (Sandbox Code Playgroud)