简体   繁体   English

为什么我无法在 Ionic 4 中创建无线电警报?

[英]Why I cannot create a radio alert in Ionic 4?

I'm trying to build an alert using Alert Controlller with Ionic 4. What I want to do is to create an array with the inputs and then create the alert and assign those inputs to it, like this:我正在尝试使用带有 Ionic 4 的Alert Controlller构建警报。我想要做的是创建一个包含输入的数组,然后创建警报并将这些输入分配给它,如下所示:

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

The problem is that I'm getting this error in Visual Studio Code:问题是我在 Visual Studio Code 中收到此错误:

> 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)

If I try to define the inputs directly in the alert, like this, it works without problem, even though it's the same array:如果我尝试直接在警报中定义输入,就像这样,它可以正常工作,即使它是相同的数组:

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' },
  ]
})

You know why this happens?你知道为什么会这样吗? I need to have the inputs array defined before the alert creation because they are generated programmatically from a remote source.我需要在创建警报之前定义输入数组,因为它们是从远程源以编程方式生成的。

Somehow, AlertInput is not automatically imported.不知何故, AlertInput不会自动导入。 However, you can import it manually.但是,您可以手动导入它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM