Angular 6 - 属性 'subscribe' 在类型 'Operator Function<{}, {}> 上不存在?
Angular 6 - Property 'subscribe' does not exist on type 'Operator Function<{}, {}>?
我用 Angular 6 创建了一个新项目,但是当我尝试添加我的 http 请求时,.subscribe 出现错误。
我只能找到 Angular 以前版本的解决方案。
有人可以帮忙吗?
//产品服务
import {Injectable} from "@angular/core";
import {Http,Response} from "@angular/http";
import {Observable} from "rxjs";
import { map, catchError } from 'rxjs/operators';
@Injectable()
export class ProductService {
constructor(public http: Http) { }
public getProducts(dataURL:string){
return this.http.get(dataURL)
.pipe(map((res:Response) => res.json())),
catchError((error:any) => Observable.throw(error || 'Server error'));
}
}
// category.component.ts
import { Component, OnInit } from '@angular/core';
import {ProductService} from "../../services/products.service";
import {Product} from "../../model/product";
import {CartService} from "../../services/cart.service";
import {Router} from "@angular/router";
@Component({
selector: 'app-category',
templateUrl: './category.component.html',
styleUrls: ['./category.component.css']
})
export class CategoryComponent implements OnInit {
public products:Array<Product>;
private sub;
constructor(
private productService:ProductService,
private cartService:CartService,
private router: Router
) { }
ngOnInit() {
this.load();
}
load = () => {
this.sub = this.productService.getProducts('./assets/mock-data/products.json')
.subscribe(res => {
this.products = res;
})
};
addToCart = (product) => {
this.cartService.addToCart({product,quantity:1})
};
ngOnDestroy() {
this.sub.unsubscribe();
}
}
将catchError
移到pipe
函数中。 catchError
是一个 rxjs 运算符。运算符需要环绕 pipe
个函数才能使用它。
return this.http.get(dataURL)
.pipe(
map((res:Response) => res.json()),
catchError((error:any) => Observable.throw(error || 'Server error'))
),
我用 Angular 6 创建了一个新项目,但是当我尝试添加我的 http 请求时,.subscribe 出现错误。 我只能找到 Angular 以前版本的解决方案。 有人可以帮忙吗?
//产品服务
import {Injectable} from "@angular/core";
import {Http,Response} from "@angular/http";
import {Observable} from "rxjs";
import { map, catchError } from 'rxjs/operators';
@Injectable()
export class ProductService {
constructor(public http: Http) { }
public getProducts(dataURL:string){
return this.http.get(dataURL)
.pipe(map((res:Response) => res.json())),
catchError((error:any) => Observable.throw(error || 'Server error'));
}
}
// category.component.ts
import { Component, OnInit } from '@angular/core';
import {ProductService} from "../../services/products.service";
import {Product} from "../../model/product";
import {CartService} from "../../services/cart.service";
import {Router} from "@angular/router";
@Component({
selector: 'app-category',
templateUrl: './category.component.html',
styleUrls: ['./category.component.css']
})
export class CategoryComponent implements OnInit {
public products:Array<Product>;
private sub;
constructor(
private productService:ProductService,
private cartService:CartService,
private router: Router
) { }
ngOnInit() {
this.load();
}
load = () => {
this.sub = this.productService.getProducts('./assets/mock-data/products.json')
.subscribe(res => {
this.products = res;
})
};
addToCart = (product) => {
this.cartService.addToCart({product,quantity:1})
};
ngOnDestroy() {
this.sub.unsubscribe();
}
}
将catchError
移到pipe
函数中。 catchError
是一个 rxjs 运算符。运算符需要环绕 pipe
个函数才能使用它。
return this.http.get(dataURL)
.pipe(
map((res:Response) => res.json()),
catchError((error:any) => Observable.throw(error || 'Server error'))
),