Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我不能在爱奥尼亚4中创建无线电警报?_Javascript_Angular_Typescript_Ionic Framework_Ionic4 - Fatal编程技术网

Javascript 为什么我不能在爱奥尼亚4中创建无线电警报?

Javascript 为什么我不能在爱奥尼亚4中创建无线电警报?,javascript,angular,typescript,ionic-framework,ionic4,Javascript,Angular,Typescript,Ionic Framework,Ionic4,我正在尝试使用Ionic 4的alert controller构建警报。我想做的是用输入创建一个数组,然后创建警报并将这些输入分配给它,如下所示: async presentAlert() { const alertInputs = [ { name: 'radio1', type: 'radio', label: 'Radio 1', value: 'value1', checked: true }, { name: 'radio2', type: 'radio', lab

我正在尝试使用Ionic 4的
alert controller
构建警报。我想做的是用输入创建一个数组,然后创建警报并将这些输入分配给它,如下所示:

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();
}

将数组强制转换为AlertInputs。使用const-alertInputs:AlertInput[]={…}谢谢,这很有效。那么,这是一个离子缺陷,迫使您手动导入接口?