2017-08-03 8 views
0

error message私は2番目の新しい人です。インジェクションエラーでサービスのプロバイダがなく、プロバイダモジュールでプロバイダを指定してもプロバイダエラーが発生しません。 コードです cribs.service.tsインジェクションエラーとプロバイダエラーなしでサービスするプロバイダがありません

import { Injectable } from '@angular/core'; 
import {Http} from '@angular/http'; 
import 'rxjs/add/operator/map'; 

@Injectable() 
export class CribsService { 

    constructor(private http: Http) { } 


     getAllCribs() 
     { 
     return this.http.get('data/Cribs.json').map(res => res.json()); 
     } 

    } 

app.module.ts

enter code here 
import { AppComponent } from './app.component'; 
import { CribsListingComponent } from './cribs-listing/cribs- 
listing.component'; 

import { CribCardComponent } from './crib-card/crib-card.component'; 
import {HttpModule} from '@angular/http'; 
import {CribsService} from './service/cribs.service'; 

    @NgModule({ 
    declarations: [ 
    AppComponent, 
    CribsListingComponent, 
    CribCardComponent, 
    ], 
    imports: [ 
     BrowserModule, 
     HttpModule 
     ], 
    providers: [CribsService], 
    bootstrap: [AppComponent] 
    }) 
    export class AppModule { } 

マイファイルheirarachyは次のとおりです。C:/ユーザー/デスクトップ/プロジェクト名/ srcに/アプリ/service/crib.service.ts c:/User/Desktop/projectname/src/app/app.module.ts

app.component.ts

import { Component } from '@angular/core'; 


@Component({ 
selector: 'app-root', 
templateUrl: './app.component.html', 
styleUrls: ['./app.component.css'], 

}) 
export class AppComponent { 
title = 'app works'; 
} 

ベビー-listing.component.ts

import { Component, OnInit } from '@angular/core'; 
import { Http } from '@angular/http'; 
import {CribsService} from './../service/cribs.service'; 
import 'rxjs/add/operator/map'; 

    @Component({ 
    selector: 'app-cribs-listing', 
    templateUrl: './cribs-listing.component.html', 
    styleUrls: ['./cribs-listing.component.css'] 
    }) 

export class CribsListingComponent implements OnInit { 

Cribs: Array<any>; 
error:String; 

constructor(private http:Http, 
private cribsservice: CribsService 
) { } 

ngOnInit() { 

this.cribsservice.getAllCribs() 
.subscribe( 
    data => this.Cribs= data, 
    error => this.error=error.statusText 
); 


} 

} 
+0

完全かつ正確なエラーメッセージを追加してください。あなたの質問に。 –

+0

@GünterZöchbauer、私はエラーメッセージのスクリーンショットを追加しました。 – PraveenRB

+0

私は、プロバイダまたはサービスを注入するファイルを含むファイルのインポートに問題があると思います。 –

答えて

1

ベロのようなコンポーネントレベルでプロバイダのサービス名を与えてみてください

@Component({ 
    selector: 'my-compoment', 
    template: '<h1>{{ title }}</h1>', 
    providers: [CribsService] 
}) 
+0

私はapp.component.tsの下にこれを書く必要がありますか? – PraveenRB

+0

はい、私はapp.component.tsの下でこれを書いて、 – PraveenRB

+0

が問題を尋ねますか?何が問題なのか – PraveenRB

関連する問題