属性 'data' 在类型 'Object' 上不存在
Property 'data' does not exist on type 'Object'
当我尝试从我的 api 中获取数据时,我的 angular 6 项目出现了这个错误。这是我的 api json 文件;
{"data":
[{"category_name":"Women Ready Made","slug":null,"image_url":"education.jpg","products":
[{"productName":"Kampala
Shorts","description":"Beautiful Handwoven Kampala Shorts","imageUrl":"C:\codes\Tradsonline\img\image1.jpg","price":2500,"discount":10},{"productName":"Flowing White Agbada","description":"Flowing white agbada with beautiful design.","imageUrl":"C:\codes\Tradsonline\img\image3.jpg","price":22000,"discount":15}]},{"category_name":"Men's Ready Made","slug":null,"image_url":"education.jpg","products":[{"productName":"Ankara Jacket","description":"Ankara Jackets with pockets.","imageUrl":"C:\codes\Tradsonline\img\image2.jpg","price":5000,"discount":15},{"productName":"Women Ankara Gown","description":"Women Ankara gown with beautiful design.","imageUrl":"C:\codes\Tradsonline\img\image4.jpg","price":8000,"discount":0}]},{"category_name":"Ankara Gowns","slug":null,"image_url":"education.jpg","products":[]}]}
这是我的服务
getCategories(): Observable<Category[]> {
return this.http.get(this.base_url + 'categories')
.pipe(map(res => {
return res.data; //this is where the error happens
}));
这是我对我的服务的导入
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Product } from '../models/product';
import { Category } from '../models/category';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
错误从何而来?
如果您没有为 http.get
调用指定泛型类型参数,则假定 object
是传递给 map
的类型。 object
没有名为 data
的 属性(您正在调用),因此会出现转译时间错误。
更改它以便在调用 http.get<T>
时在泛型类型约束中定义返回的结构。
getCategories(): Observable<Category[]> {
return this.http.get<{data: Category[]}>(this.base_url + 'categories')
.pipe(map(res => {
return res.data; // now .data is known and is expected to contain Category[]
}));
}
当我尝试从我的 api 中获取数据时,我的 angular 6 项目出现了这个错误。这是我的 api json 文件;
{"data":
[{"category_name":"Women Ready Made","slug":null,"image_url":"education.jpg","products":
[{"productName":"Kampala
Shorts","description":"Beautiful Handwoven Kampala Shorts","imageUrl":"C:\codes\Tradsonline\img\image1.jpg","price":2500,"discount":10},{"productName":"Flowing White Agbada","description":"Flowing white agbada with beautiful design.","imageUrl":"C:\codes\Tradsonline\img\image3.jpg","price":22000,"discount":15}]},{"category_name":"Men's Ready Made","slug":null,"image_url":"education.jpg","products":[{"productName":"Ankara Jacket","description":"Ankara Jackets with pockets.","imageUrl":"C:\codes\Tradsonline\img\image2.jpg","price":5000,"discount":15},{"productName":"Women Ankara Gown","description":"Women Ankara gown with beautiful design.","imageUrl":"C:\codes\Tradsonline\img\image4.jpg","price":8000,"discount":0}]},{"category_name":"Ankara Gowns","slug":null,"image_url":"education.jpg","products":[]}]}
这是我的服务
getCategories(): Observable<Category[]> {
return this.http.get(this.base_url + 'categories')
.pipe(map(res => {
return res.data; //this is where the error happens
}));
这是我对我的服务的导入
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Product } from '../models/product';
import { Category } from '../models/category';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
错误从何而来?
如果您没有为 http.get
调用指定泛型类型参数,则假定 object
是传递给 map
的类型。 object
没有名为 data
的 属性(您正在调用),因此会出现转译时间错误。
更改它以便在调用 http.get<T>
时在泛型类型约束中定义返回的结构。
getCategories(): Observable<Category[]> {
return this.http.get<{data: Category[]}>(this.base_url + 'categories')
.pipe(map(res => {
return res.data; // now .data is known and is expected to contain Category[]
}));
}