0
剛剛開始使用角度2,並遇到我的第一個錯誤。我試圖創建一個從服務器獲取數據的服務,但我得到這個錯誤如何解決無法解決角度上的所有參數錯誤
Error: Can't resolve all parameters for CourseService: (?). at SyntaxError.ZoneAwareError (http:……}
這是服務代碼引發錯誤的應用程序/場/ courses.service.ts
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();
});
}
}
這服務應該在app /課程中使用/ courses.component.ts
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;
}
}
和app.module包含
....
....
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 { }
用您的應用程序的導入語句更新您的應用程序的帖子?(所有組件和服務,如果有的話) – Aravind