首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular2 RC5 ngModule: NameService没有提供程序

Angular2 RC5 ngModule: NameService没有提供程序
EN

Stack Overflow用户
提问于 2016-08-13 00:46:58
回答 1查看 2.7K关注 0票数 8

我有个小(大.)angular2服务的问题。我试图使用ngModule提供者选项提供服务,但当我尝试将其放入组件中时,我得到了:(这里是RankingService)。

app.module.ts

代码语言:javascript
复制
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 { }

ranking.service.ts

代码语言:javascript
复制
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
    }
}

ranking.component.ts

代码语言:javascript
复制
/*
 * 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 )
    }
}

我看过很多次了,但是我看不出问题所在,谢谢你的帮助;-)

编辑:当我直接在我的组件中提供服务时,它是有效的,只提供给应用模块不起作用。

编辑2:我解决了我的问题,我的系统‘t配置是错误的,或者更确切地说不是为了管理这种情况:我已经创建了我自己的包( Twinipix ),但是我的应用程序不包含Twinipix文件夹,而是包含公用文件夹,所以问题来自jspm.config.js文件:

代码语言:javascript
复制
packages[ "Twinipix" ] = {
    main: "../public/main.js"
};

我使用这个配置来做更多的逻辑导入(导入整个应用程序,而不是一个文件),这只是我完美主义的一面!因此,对于更常见的系统So配置,所有这些都能完美地工作!

EN

回答 1

Stack Overflow用户

发布于 2017-04-01 11:36:35

回答了提问者:

我解决了我的问题,是我的系统‘t配置出错了,或者更确切地说不是为了管理这种情况:我已经创建了自己的包( Twinipix ),但是我的应用程序不包含Twinipix文件夹,而是包含公用文件夹,所以问题来自于jspm.config.js文件:

代码语言:javascript
复制
packages[ "Twinipix" ] = {
    main: "../public/main.js"
};

我使用这个配置来执行更多的逻辑导入(导入整个应用程序,而不是一个文件),这只是我完美主义的一面!因此,对于更常见的系统So配置,所有这些都能完美地工作!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38928141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档