类型不可分配给类型 providedIn 属性 ProvidedIn 丢失
Type is not assignable to type providedIn Property ProvidedIn is missing
我正在尝试借助 Angular 的教程 https://angular.io/guide/http
创建服务
我正在使用 Angular 7.0.7
。
该服务将从 url:
中获取 json 数据
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable
export class ProductListService {
constructor(private http: HttpClient) {
}
productListUrl: string = "https://blahblah.com/products";
getProductList() {
return this.http.get(this.productListUrl);
}
}
我在 @Injectable()
下看到了一条带有此问题标题的波浪线。为什么会发生这种情况,我该如何解决?
我有一个组件将使用此服务:
import { ProductListService } from '../services/productList.service';
@Component({
selector: 'app-productlist',
templateUrl: './productlist.component.html',
styleUrls: ['./productlist.component.css'],
providers: [ProductListService]
})
您错过了为 @Injectable()
装饰器添加 parenthesis
@Injectable()
export class ProductListService {
constructor(private http: HttpClient) {
}
您的@Injectable
不正确。添加 providedIn
属性 引用您将使用该服务的组件:
@Injectable({
providedIn: 'app-productlist',
})
export class ProductListService {
constructor(private http: HttpClient) {
}
另一个解决方案是像这样使用装饰器:@Injectable()
,并在 app.module.ts
文件中声明您的服务,作为提供者
@Injectable()
export class ProductListService {
constructor(private http: HttpClient) {
}
我正在尝试借助 Angular 的教程 https://angular.io/guide/http
创建服务我正在使用 Angular 7.0.7
。
该服务将从 url:
中获取 json 数据import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable
export class ProductListService {
constructor(private http: HttpClient) {
}
productListUrl: string = "https://blahblah.com/products";
getProductList() {
return this.http.get(this.productListUrl);
}
}
我在 @Injectable()
下看到了一条带有此问题标题的波浪线。为什么会发生这种情况,我该如何解决?
我有一个组件将使用此服务:
import { ProductListService } from '../services/productList.service';
@Component({
selector: 'app-productlist',
templateUrl: './productlist.component.html',
styleUrls: ['./productlist.component.css'],
providers: [ProductListService]
})
您错过了为 @Injectable()
装饰器添加 parenthesis
@Injectable()
export class ProductListService {
constructor(private http: HttpClient) {
}
您的@Injectable
不正确。添加 providedIn
属性 引用您将使用该服务的组件:
@Injectable({
providedIn: 'app-productlist',
})
export class ProductListService {
constructor(private http: HttpClient) {
}
另一个解决方案是像这样使用装饰器:@Injectable()
,并在 app.module.ts
文件中声明您的服务,作为提供者
@Injectable()
export class ProductListService {
constructor(private http: HttpClient) {
}