2017-08-04 22 views
0

角度2で始動し、最初のエラーが発生しました。私はこの解決方法角度ですべてのパラメータを解決できません

import { Http } from '@angular/http'; 

export class CourseService { 

    private _courses : Object[]; 

    public get courses() : Object[] { 
     return this._courses; 
    } 

    public set courses(v : Object[]) { 
     this._courses = v; 
    } 

    constructor(private http: Http) { 
     http.get("https://jsonplaceholder.typicode.com/posts").subscribe(data => { 
      this._courses = data.json(); 
     }); 
    } 
} 

をサーバからデータをフェッチするためのサービスを作成しようとしていますが、

Error: Can't resolve all parameters for CourseService: (?). at SyntaxError.ZoneAwareError (http:……}

このエラーを取得しています、これは誤りアプリ/コース/ courses.service.tsを投げるサービスコードである

サービスは、/ app /コースで使用されることになって

import { Component } from '@angular/core' 
import { CourseService } from "app/courses/course.service"; 

@Component({ 
    selector: 'courses', 
    providers: [CourseService], 
    templateUrl: './courses.component.html', 
    styleUrls: ['./courses.component.css'] 
}) 

export class CoursesComponent { 
    title : string = "The title of the Course"; 
    courses : Object[]; 

    constructor(private courseService: CourseService) { 
     console.log("hello"); 
     this.courses = courseService.courses; 
    } 
} 

courses.component.tsとapp.moduleは

が含まれています3210
.... 
.... 
import { AppComponent } from './app.component'; 
import { CoursesComponent } from './courses/courses.component'; 
import { CourseService } from "./courses/course.service"; 

@NgModule({ 
    declarations: [ 
     AppComponent, 
     CoursesComponent, 
    ], 
    imports: [ 
     BrowserModule, 
     FormsModule, 
     HttpModule 
    ], 
    providers: [CourseService], 
    bootstrap: [AppComponent, CoursesComponent] 
}) 
export class AppModule { } 
+1

このサービスのインポートステートメントは、アプリケーション全体にわたって更新してください(すべてのコンポーネントとサービスがあればそれを使用してください) – Aravind

答えて

1

エクスポートクラスの前に@Injectable()を挿入します。

関連する問題