Angular 类型';未定义[]和#x27;无法转换为类型';OrderArray';。财产';Menuame';类型'中缺少;未定义[]和#x27; 目标
您好,我正在创建一个餐厅订购应用程序。我能从firebase中读取一个可观察的列表。现在,我想使用离子选择每个菜单分配数量的数量有人想订购 问题 现在,我只想将订单的数量分配给一个模型。我已经创建了一个菜单顺序模型,名为OrderArray,我希望将此模型作为数组。这是订单阵列的型号。ts:Angular 类型';未定义[]和#x27;无法转换为类型';OrderArray';。财产';Menuame';类型'中缺少;未定义[]和#x27; 目标,angular,ionic-framework,Angular,Ionic Framework,您好,我正在创建一个餐厅订购应用程序。我能从firebase中读取一个可观察的列表。现在,我想使用离子选择每个菜单分配数量的数量有人想订购 问题 现在,我只想将订单的数量分配给一个模型。我已经创建了一个菜单顺序模型,名为OrderArray,我希望将此模型作为数组。这是订单阵列的型号。ts: export interface OrderArray { $key?: string, menuName: string, menuPrice: string, quantity: st
export interface OrderArray {
$key?: string,
menuName: string,
menuPrice: string,
quantity: string
}
export interface Order {
custName: string,
custPhone: string,
restoName: string,
restoPhone: string,
restoAddress: string,
message: string,
orderArray: string
// timestamp: number
}
然后我想将这个数组转换成字符串,并将它分配给另一个名为Order的模型。这是型号订单。ts
:
export interface OrderArray {
$key?: string,
menuName: string,
menuPrice: string,
quantity: string
}
export interface Order {
custName: string,
custPhone: string,
restoName: string,
restoPhone: string,
restoAddress: string,
message: string,
orderArray: string
// timestamp: number
}
当我加载页面时出现问题:
无法将类型“undefined[]”转换为类型“OrderArray”。财产
类型“undefined[]”中缺少“menuName”
还有一个错误
类型“OrderArray”不可分配给类型“string”
我的代码
这是订购菜单。ts
import { Order } from './../../models/order';
import { OrderArray } from './../../models/order-array';
.
.
.
public orderArray = [] as OrderArray;
.
.
.
this.order.orderArray = this.orderArray;
这是订单菜单.html
<ion-header>
<ion-navbar color="primary">
<ion-title>OrderMenu</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item *ngFor="let data of menuData | async ; let i = index ">
<div item-content>
<h2>Menu Name: {{data.payload.val().menuName}}</h2>
<p>Price: {{data.payload.val().menuPrice}}</p>
<ion-item>
<ion-label>
Jumlah Porsi:
</ion-label>
<ion-select [(ngModel)]="orderArray[i]">
<ion-option value="1">1</ion-option>
<ion-option value="2">2</ion-option>
</ion-select>
</ion-item>
</div>
</ion-item>
</ion-list>
<ion-item item-content>
<ion-label floating>Catatan</ion-label>
<ion-input type="text" [(ngModel)]="order.message"></ion-input>
</ion-item>
<button ion-button (click)="orderMenu()">Order</button>
</ion-content>
订单菜单
菜单名:{{data.payload.val().menuName}
价格:{data.payload.val().menuPrice}
朱姆拉·波西:
1.
2.
卡塔坦
命令
任何帮助都将不胜感激 谢谢@Jacopo Sciampi的回答 而不是
public orderArray = [] as OrderArray;
我应该用
public orderArray: OrderArray[] = []
this.order.orderArray = this.orderArray.toString();
然后
而不是
this.order.orderArray = this.orderArray;
我应该用
public orderArray: OrderArray[] = []
this.order.orderArray = this.orderArray.toString();
orderArray:string
orderArray是string,所以this.order.orderArray=this.orderArray
将抛出错误应该是公共orderArray:orderArray[]=[]
这样您就不会得到未定义的错误