作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试根据某个元素从数组中获取值,然后获取完全匹配的元素。但不知何故,我在这个函数 getProduct(name1: string) 中遇到了这个错误,我已经导入了 map 。帮助将不胜感激。this.searchProduct(...).subscribe 不是函数
products: Product[] = [];
product: Product;
searchProduct(name2: string): Observable<Product[]> {
var config = {
headers: {
'Content-Type': 'application/json'
}
};
const ans = name2;
let obj = { name_product: ans }
let body = JSON.stringify(obj);
let header = new HttpHeaders();
header = header.append('Content-Type','application/json');
return this.httpClient.post('postRestService', body, config).subscribe(res => {
this.products = res;
console.log(this.products);
})
}
getProduct(name1: string): Observable<Product> {
return this.searchProduct(name1).subscribe(products =>
products.find(product => product.name1 == name1));
}
最佳答案
http 服务从 angular 2 更改为 angular 4。在 angular 4 中你不需要 map :
products: Product[] = [];
product: Product;
searchProduct(name2: string): Observable<Product[]> {
var config = {
headers: {
'Content-Type': 'application/json'
}
};
const ans = name2;
let obj = { name_product: ans }
let body = JSON.stringify(obj);
let header = new HttpHeaders();
header = header.append('Content-Type','application/json');
this.http.post('postRestService', body, config).subscribe(res => {
this.products = res;
console.log(this.products);
})
}
getProduct(name1: string): Observable<Product> {
return this.searchProduct(name1).subscribe(products => products.find(product => product.name1 == name1));
}
关于javascript - 错误类型错误 : subscribe is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49921329/
我是一名优秀的程序员,十分优秀!