如何解决无法解决角度上的所有参数错误

时间:2017-08-04 23:07:49

标签: javascript angular

刚刚开始使用角度2,并遇到我的第一个错误。我正在尝试创建一个服务从服务器获取数据,但我收到此错误

  

错误:无法解析CourseService的所有参数:(?)。在SyntaxError.ZoneAwareError(http:......}

这是抛出错误app / courses / 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 / 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 { }

1 个答案:

答案 0 :(得分:3)