Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
964 views
in Technique[技术] by (71.8m points)

angular - Type Script error TS2554: Expected 0 arguments, but got 1. in ionic application

Good day, everyone. I am facing this problems in my project which is Type Script error TS2554: Expected 0 arguments, but got 1. This error make me cannot create a select the other option for custom input pop up. In this forum, I have post the typescript code and error part of the typecript code. The error code of the typescript

Typescript Code

 import { Validators, FormBuilder, FormGroup, FormControl } from '@angular/forms';
  import { AlertController } from '@ionic/angular';

  constructor(
    public formBuilder: FormBuilder,
    public alertController: AlertController
  ) { }

      ngOnInit() {
      this.religions = [
          "Islam",
          "Buddha",
          "Hinduism",
          "Christian",
          "Sikhism",
          "Taiosm",
          "Other"
        ];
        
        this.currentReligionValue = "Islam";
      
        this.validations_form = this.formBuilder.group({
        religion: new FormControl(this.currentReligionValue, Validators.required),
    });
    }

      validation_messages = {
    'religion': [
      { type: 'required', message: 'Religion is required to select' },
  };

  //When have other values
  selectChanged(selected) {
    if (selected === 'Other') {
      this.inputValue();
    } else {
      this.currentvalue = selected;
    };
  };

The error part in the TypeScript

async inputValue() {
        const inputAlert = await this.alertController.create({
          header: 'Enter your custom color:',
          inputs: [ { type: 'text', placeholder: 'type in' } ],
          buttons: [ { text: 'Cancel' }, { text: 'Ok' } ]
        });
      inputAlert.onDidDismiss((data) => { //<-- The error part is starting here
          let customName: string = data.data.values[0];
          if (customName) {
            let indexFound = this.religions.findIndex(religion => religion === customName)
            if (indexFound === -1) {
              this.religions.push(customName);
              this.currentReligionValue = customName;
            } else {
              this.currentReligionValue = this.religions[indexFound];
            };
          };      
        }).catch(err=>console.log(err));
        await inputAlert.present();
      };
question from:https://stackoverflow.com/questions/65846565/type-script-error-ts2554-expected-0-arguments-but-got-1-in-ionic-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to the documentation, the method onDidDismiss<T = any>() does not take any parameter.
It returns a Promise of OverlayEventDetail<T> that resolves when the alert did dismiss.

The following code should work just fine.

const inputAlert = await this.alertController.create({...});
inputAlert.onDidDismiss()
  .then((event: OverlayEventDetail) => event.data...)
  .catch(console.log);

You can type the method if you want auto-completion when accessing the data in the callback.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...