@Injectable标签Angular 5的TS错误

时间:2017-12-08 15:58:33

标签: angular typescript angular5

编译代码时遇到此错误:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable()
export class TaskService {
  constructor(private http: HttpClient) {
  }

  getTasks() {
    return this.http.get('/api/tasks');
  }
}

src / app / tasks / task.service.ts(4,1)中的错误:错误TS1238:当作为表达式调用时,无法解析类装饰器的签名。

我不确定它出错的地方...... 4,1是Injectable之前的@符号。

1 个答案:

答案 0 :(得分:1)

尝试添加return类型getTasks(): Observable<any>。 你正确地注射了HttpClientModule吗?

import { HttpClientModule } from '@angular/common/http';

@NgModule({
  imports: [
    BrowserModule,
    // Include it under 'imports' in your application module
    // after BrowserModule.
    HttpClientModule,
  ],
})