gpt4 book ai didi

angular - 类型 'Observable' 不可分配给类型 '[]'

转载 作者:太空狗 更新时间:2023-10-29 16:59:53 27 4
gpt4 key购买 nike

我是 Angular2/TypeScript 的新手,所以如果我犯了一些愚蠢的错误,请原谅。我想做的是从 Select 下拉菜单中使用返回 JSON 数组的服务填充数据。

这是我的代码:

product-maintenance.component.ts

import { Component, OnInit} from '@angular/core';
import { ProductMaintenanceService } from '../../_services/product-maintenance.service';
import { ModuleConst } from '../../../data/const';
import { Product } from './product-types';
import { Products } from './products';

@Component({
selector: 'app-product-maintenance',
templateUrl: './product-maintenance.component.html',
styleUrls: ['./product-maintenance.component.css']
})

export class ProductMaintenanceComponent implements OnInit {
selectedProduct: Product = new Product(0, 'Insurance');
products: Product[];
productList: Products[];

constructor( private productService: ProductMaintenanceService ) {

}

ngOnInit() {
// Dropdown list
this.products = this.productService.getProductTypeList();
}
// Populate data using onSelect method
onSelect(typeid) {
this.productList = this.productService.getProducts()
.filter((item)=>item.ptypeid == typeid);
}
}

product-type.ts (用于填充下拉列表):

export class Product {
constructor(
public ptypeid: number,
public name: string
) { }
}

products.ts (用于从服务填充数据):

export class Products {
constructor(
public id: number,
public ptypeid: number,
public name: string,
public currency: string,
public state: string,
public riskRating: number,
public clientSegment: string,
public aiStatus: string,
public premiumType: string,
public tenure: number
) { }
}

product-maintenance.service.ts :

import { Injectable, Inject } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
import { APP_CONFIG, IAppConfig } from '../app.config';
import { Products } from './products';
import { Product } from './product-types';

@Injectable()
export class ProductMaintenanceService {
result: Array<Object>;
constructor(
@Inject(APP_CONFIG) private config: IAppConfig,
private http: Http
) { }

private productURL = 'data/productList.json';

// Get product list
getProducts() : Observable<any> {
// ...using get request
return this.http.get(this.productURL)
// ...and calling .json() on the response to return data
.map((response: Response) => {
console.log(response);
return response.json();
});
}


getProductTypeList() {
return [
new Product(1, 'Unit Trust'),
new Product(2, 'Insurance'),
new Product(3, 'Structured Deposit'),
new Product(4, 'Bonds'),
new Product(5, 'DCI'),
new Product(6, 'SP')
];
}
}

product-maintenance.component.html :

<table>
<tr *ngFor="let item of productList">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.currency }}</td>
<td>{{ item.state }}</td>
<td>{{ item.riskRating }}</td>
<td>{{ item.clientSegment }}</td>
<td>{{ item.aiStatus }}</td>
<td>{{ item.premiumType }}</td>
<td>{{ item.tenure }}</td>
</tr>
</table>

productList.json :

[
{
"ptypeid": 1,
"id": 1,
"name": "Unit Trust 1",
"currency": "SGD",
"state": "Active",
"riskRating": 1,
"clientSegment": "Retail/Premier",
"aiStatus": "No",
"premiumType": "Regular Premium",
"tenure": 5
},
{
"ptypeid": 2,
"id": 2,
"name": "Unit Trust 2",
"currency": "SGD",
"state": "Active",
"riskRating": 3,
"clientSegment": "Retail/Premier",
"aiStatus": "No",
"premiumType": "Single/Lumpsum Premium",
"tenure": 10
}
]

如果我定义我的 getProducts()作为getProductTypeList()然后它完美地填充了我 View 中的数据(如果我从下拉列表中选择 Unit trust 那么它应该填充相关数据)。但是,如果我改为使用 api url,则会出现以下错误:

Type 'Observable<any>' is not assignable to type 'Products[]'. Property 'length' is missing in type 'Observable<any>'

我不明白如何解决这个错误。谁能帮我解决这个问题。提前致谢。

最佳答案

productList应该是 Product[]不是Observable<any> .所以分配 productList来自 getProducts 的值订阅方法,您将在其中检索 Product 的数组

onSelect(typeid) {
this.productService.getProducts()
.pipe(
filter((item)=>item.ptypeid == typeid)
)
.subscribe((products) => {
this.productList = products;
});
}

关于angular - 类型 'Observable<any>' 不可分配给类型 '[]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41819805/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com