在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)
零件:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styles: [':host(.selected) {display:block;background:#ccc;}']
})
export class AppComponent {
title = 'app works!!';
constructor(private otherSer: OtherService){};
}
Run Code Online (Sandbox Code Playgroud)
我遇到问题,因为"在组件'app.component'中找不到名字'OtherService'".
如果我做错了请纠正我.
谢谢!
| 归档时间: |
|
| 查看次数: |
863 次 |
| 最近记录: |