我是離子的新手。我想ionic-alert在按下 ok 按鈕后獲取輸入值,如下圖所示。

我嘗試使用以下代碼:
async presentAlertPrompt() {
const alert = await alertController
.create({
cssClass: 'my-custom-class',
header: 'Confirmation',
inputs: [
{
name: 'note',
value: '',
placeholder: 'Note desciptive',
},
{
name: 'quantity',
type: 'number',
value: '',
min: 1,
label: 'Quanité',
placeholder: 'Quanité'
},
{
name: 'custome_price',
type: 'number',
value: '',
min: 0,
label: 'Prix à discuter',
placeholder: 'Prix à discuter',
},
{
name: 'customer',
placeholder: 'Nom du client',
value: '',
},
{
name: 'phone',
type: 'phone',
placeholder: 'Téléphone du client',
value: '',
},
],
buttons: [
{
text: 'Annuler',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
console.log('Confirm Cancel')
},
},
{
text: 'Ok',
handler: () => {
console.log('Confirm Ok ')
},
},
],
});
return alert.present();
},
但是當我從另一個方法呼叫這個方法時,我得到的是 promise 物件而不是輸入值:
onSelling(){
const confirm = this.presentAlertPrompt()
console.log('confirm >>>>>>>>>> : ', confirm)
//this.getCurrentPosition()
},
確認 >>>>>>>>>> : Promise { : "pending" } : "fulfilled" : undefined : Promise.prototype { ... }
我也嘗試使用then,Async Await但我得到 了undefined.
onSelling(){
this.presentAlertPrompt().then((res) => {
console.log('res ->>>>>>>>>>>>>>> ', res)
},
(err) => {
console.log('err ->>>>>>>>>>>>>>> ', err)
})
},
uj5u.com熱心網友回復:
要解決您的問題,您需要在 ok 按鈕處理程式回呼中添加一個引數。
...
{
text: 'Ok',
handler: (alertData) => {
console.log('Confirm Ok ----------> : ', alertData)
},
},
...
輸出
確認 OK :
Object { note: "Note", quantity: "5", custome_price: "4500", customer: "Ged Flod", phone: "" }
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/450293.html
