为什么我无法在 Ionic 4 中创建无线电警报?
Why I cannot create a radio alert in Ionic 4?
我正在尝试使用 Alert Controlller
和 Ionic 4 构建警报。我想要做的是创建一个包含输入的数组,然后创建警报并将这些输入分配给它,就像这样:
async presentAlert() {
const alertInputs = [
{ name: 'radio1', type: 'radio', label: 'Radio 1', value: 'value1', checked: true },
{ name: 'radio2', type: 'radio', label: 'Radio 2', value: 'value2' },
]
const alertMessage = await this.alertCtrl.create({
inputs: alertInputs
});
}
问题是我在 Visual Studio 代码中遇到了这个错误:
> Type '({ name: string; type: string; label: string; value: string;
> checked: boolean; } | { name: string; type: string; label: string;
> value: string; checked?: undefined; })[]' is not assignable to type
> 'AlertInput[]'. Type '{ name: string; type: string; label: string;
> value: string; checked: boolean; } | { name: string; type: string;
> label: string; value: string; checked?: undefined; }' is not
> assignable to type 'AlertInput'.
> Type '{ name: string; type: string; label: string; value: string; checked: boolean; }' is not assignable to type 'AlertInput'.
> Types of property 'type' are incompatible.
> Type 'string' is not assignable to type '"number" | "radio" | "date" | "email" | "password" | "search" | "tel" | "text" | "url" |
> "time" | "checkbox" | "textarea"'.ts(2322)
如果我尝试直接在警报中定义输入,就像这样,它可以正常工作,即使它是同一个数组:
const alertMessage = await this.alertCtrl.create({
inputs: [
{ name: 'radio1', type: 'radio', label: 'Radio 1', value: 'value1', checked: true },
{ name: 'radio2', type: 'radio', label: 'Radio 2', value: 'value2' },
]
})
你知道为什么会这样吗?我需要在创建警报之前定义输入数组,因为它们是从远程源以编程方式生成的。
不知为何,AlertInput
没有自动导入。但是,您可以手动导入它。
import {AlertInput} from '@ionic/core/dist/types/components/alert/alert-interface';
...
async presentAlert() {
const inputs: AlertInput[] = [
{ name: 'radio1', type: 'radio', label: 'Radio 1', value: 'value1', checked: true },
{ name: 'radio2', type: 'radio', label: 'Radio 2', value: 'value2' }
];
const alertMessage = await this.alertCtrl.create({ inputs });
await alertMessage.present();
}
我正在尝试使用 Alert Controlller
和 Ionic 4 构建警报。我想要做的是创建一个包含输入的数组,然后创建警报并将这些输入分配给它,就像这样:
async presentAlert() {
const alertInputs = [
{ name: 'radio1', type: 'radio', label: 'Radio 1', value: 'value1', checked: true },
{ name: 'radio2', type: 'radio', label: 'Radio 2', value: 'value2' },
]
const alertMessage = await this.alertCtrl.create({
inputs: alertInputs
});
}
问题是我在 Visual Studio 代码中遇到了这个错误:
> Type '({ name: string; type: string; label: string; value: string;
> checked: boolean; } | { name: string; type: string; label: string;
> value: string; checked?: undefined; })[]' is not assignable to type
> 'AlertInput[]'. Type '{ name: string; type: string; label: string;
> value: string; checked: boolean; } | { name: string; type: string;
> label: string; value: string; checked?: undefined; }' is not
> assignable to type 'AlertInput'.
> Type '{ name: string; type: string; label: string; value: string; checked: boolean; }' is not assignable to type 'AlertInput'.
> Types of property 'type' are incompatible.
> Type 'string' is not assignable to type '"number" | "radio" | "date" | "email" | "password" | "search" | "tel" | "text" | "url" |
> "time" | "checkbox" | "textarea"'.ts(2322)
如果我尝试直接在警报中定义输入,就像这样,它可以正常工作,即使它是同一个数组:
const alertMessage = await this.alertCtrl.create({
inputs: [
{ name: 'radio1', type: 'radio', label: 'Radio 1', value: 'value1', checked: true },
{ name: 'radio2', type: 'radio', label: 'Radio 2', value: 'value2' },
]
})
你知道为什么会这样吗?我需要在创建警报之前定义输入数组,因为它们是从远程源以编程方式生成的。
不知为何,AlertInput
没有自动导入。但是,您可以手动导入它。
import {AlertInput} from '@ionic/core/dist/types/components/alert/alert-interface';
...
async presentAlert() {
const inputs: AlertInput[] = [
{ name: 'radio1', type: 'radio', label: 'Radio 1', value: 'value1', checked: true },
{ name: 'radio2', type: 'radio', label: 'Radio 2', value: 'value2' }
];
const alertMessage = await this.alertCtrl.create({ inputs });
await alertMessage.present();
}