2016-08-13 81 views
8

Angular2 servisleri ile küçük (büyük ...) bir sorun yaşıyorum. ngModule sağlayıcıları seçenekleri ile hizmet sunmaya çalışıyorum, ancak bileşenlerim içine almayı denediğimde: ServiceName sağlayıcısı yok (burada, RankingService).Angular2 RC5 ngModule: Sağlayıcı için NameService

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 { } 

ranking.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 
    } 
} 

ranking.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) 
    } 
} 

Birçok kez açısal doktora baktım ama problemi görmüyorum, yardımın için teşekkür ederim ;-)

Düzenleme: Doğrudan bileşenlerim arasında hizmet sağladığımda, çalışır, sadece uygulama modülüne girmez. t.

Düzenleme 2: Sorunumu çözdüm, yanlış olan ya da bu tür bir durumu yönetmek için tasarlanmamış olan sistemimdeki yapılandırmamdı: Kendi paketimi oluşturdum (Twinipix) ancak uygulamam Twinipix içermiyor klasör ziyade ortak klasör, sorun jspm.config.js dosyasından geldi bu yüzden:

packages[ "Twinipix" ] = { 
    main: "../public/main.js" 
}; 

Ben daha mantıklı ithalatı (tüm uygulama ve bir tek dosya içe) yapmak için bu yapılandırmayı kullanılan bu juste benim mükemmeliyetçi yan! Yani, daha yaygın bir systemjs yapılandırmasıyla, hepsi mükemmel çalışıyor! Soru asker tarafından Yanıtlanmış

+2

her şey neredeyse tamam görünüyor. Rc5 sonra koddan bir hata bulmak çok zor olduğundan lütfen plunker sağlayın. – micronyks

+0

Strangely, plunker ile çalışır, neden anlamıyorum ... bu [plunker] kodu (https://plnkr.co/edit/Uhf2z23bVDOaE9nVW4yO?p=preview), konsolunuzu izleyebilir ve gözlemleyebilirsiniz. İçinde birden çok nesneyle oturum açın, bu ** RankingService ** 'den dönüş. Kodu sadeleştirdim (sadece sorunlu bileşen ve servis) –

cevap

0

: Kendi paketini oluşturduk (Twinipix: Ben sorunu çözüldü

, yanlış, daha doğrusu değil durum bu tür yönetmek için tasarlanmış benim systemjs yapılandırma oldu

packages[ "Twinipix" ] = { 
    main: "../public/main.js" 
}; 

Ben tüm uygulama (daha mantıklı ithalat yapmak ithalat bu yapılandırmayı kullanılan ve: sorun jspm.config.js dosyasından geldi bu yüzden) ancak başvurum, Twinipix klasörü ziyade ortak klasörü içermiyor tek bir dosya), sadece mükemmeliyetçi tarafım! Yani, daha yaygın bir systemjs yapılandırması ile, tüm mükemmel çalışır!