angular學習006在angular6.0.8使用ng generate service建立服務報錯

lich0609發表於2018-06-10

1、使用ng generate service建立服務

D:\angular\my-app\src\app\service>ng generate service test
CREATE src/app/service/test.service.spec.ts (362 bytes)
CREATE src/app/service/test.service.ts (133 bytes)

2、使用webtorm開啟直接報錯

test.service..ts

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

@Injectable({
  providedIn: 'root'
})
export class TestService {

  constructor() { }
}

錯誤如下
Argument type {providedIn: "root"} is not assignable to parameter type {providedIn: Type<any> | "root" | null} & InjectableProvider less... (Ctrl+F1) 
Checks TypeScript called function parameters , return values , assigned expressions to be of correct type.

3、解決辦法(還不清楚原來)

刪除@Injectable小括號中的內容

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

@Injectable()
export class TestService {

  constructor() { }
}

相關文章