我在angular2服务中遇到了一个小问题(大问题)。
我正在尝试使用ngModule提供程序选项提供服务,但是当我尝试将其添加到组件中时,我得到了:没有ServiceName提供程序(此处为RankService)。
app.module.ts
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule } from '@angular/forms'
import { HttpModule } from '@angular/http'
/*
* App component and routing
*/
import { AppComponent } from './components/app/app.component'
import { routing } from './app.routes'
/*
* Services
*/
import { UserService } from './services/user.service'
import { RankingService } from './services/ranking.service'
/*
* Global components
*/
import { HeaderComponent } from './components/header/header.component'
import { FooterComponent } from './components/footer/footer.component'
/*
* App Components
*/
import { RankingComponent } from './components/ranking/ranking.component'
import { OthersComponent } from './components/others/others.component'
import { IpixsComponent } from './components/ipix/ipixs.component'
import { IpixDetailsComponent } from './components/ipix/ipix-details/ipix-details.component'
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
routing
],
declarations: [
AppComponent,
HeaderComponent,
FooterComponent,
RankingComponent,
OthersComponent,
IpixsComponent,
IpixDetailsComponent
],
providers: [
UserService,
RankingService
],
exports: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
rank.service.ts
import { Injectable } from '@angular/core'
/*
* Entities
*/
import { RankingUser } from './../entities/ranking-user'
@Injectable()
export class RankingService {
getRanking() {
const users: RankingUser[] = [
{ user: { picture : '', name: 'Edwige Chou' }, correlation: 100 },
{ user: { picture : '', name: 'Mathieu Vandeginste' }, correlation: 78 },
{ user: { picture : '', name: 'Isabelle Isa' }, correlation: 51 },
{ user: { picture : '', name: 'Julien Sergent' }, correlation: 39 },
{ user: { picture : '', name: 'Paul Raul' }, correlation: 23 },
{ user: { picture : '', name: 'Johnatan' }, correlation: 17 }
]
return users
}
}
rank.component.ts
/*
* Dependencies
*/
import { Component, OnInit } from '@angular/core'
/*
* Services
*/
import { RankingService } from './../../services/ranking.service'
/*
* Entities
*/
import { RankingUser } from './../../entities/ranking-user'
@Component({
moduleId: module.id,
selector: 'ranking',
templateUrl: 'ranking.component.html',
styleUrls: [ 'ranking.component.css' ]
})
export class RankingComponent implements OnInit {
ranking: RankingUser[]
constructor(
private rankingService: RankingService
) { }
ngOnInit() {
this.getRanking()
}
getRanking() {
this.ranking = this.rankingService.getRanking()
console.log( this.ranking )
}
}
我看了很多遍 Angular 文档,但看不到问题,谢谢您的帮助;-)
编辑:当我直接在组件中提供服务时,它是有效的,仅在应用程序模块中不提供它。
编辑2:我解决了我的问题,那是我的systemjs配置错了,或者不是想解决这种情况:我创建了自己的包(Twinipix),但是我的应用程序不包含Twinipix文件夹,而是公共(public)的文件夹,因此问题来自jspm.config.js文件:
packages[ "Twinipix" ] = {
main: "../public/main.js"
};
我使用该配置进行了更多的逻辑导入(导入整个应用程序而不是单个文件),这只是我的完美主义者!
因此,使用更通用的systemjs配置,所有功能都能完美运行!
最佳答案
由提问者回答:
我解决了我的问题,那是我的systemjs配置错了,或者不是想解决这种情况:我创建了自己的包(Twinipix),但是我的应用程序不包含Twinipix文件夹,而是公共(public)文件夹,因此问题来自jspm.config.js文件:
packages[ "Twinipix" ] = {
main: "../public/main.js"
};
我使用该配置进行了更多的逻辑导入(导入整个应用程序而不是单个文件),这只是我的完美主义者!因此,使用更通用的systemjs配置,所有功能都能完美运行!