简体   繁体   English

在离子(离子选择器)的服务中从 function 返回 object

[英]Return object from function in a service in ionic (ionic picker)

In my app, I would like to have a different picker , with two or three columns, so I made 2 different functions to simplify the code.在我的应用程序中,我想有一个不同的picker ,有两列或三列,所以我制作了 2 个不同的函数来简化代码。 Now I'd like to return my object with my result when the Validate button is pressed.现在我想在按下Validate button时返回我的 object 和我的结果。 I think I need to deal with Promise , but I am not so familiar with it and I think I am doing something wrong.我想我需要处理Promise ,但我不太熟悉它,我认为我做错了什么。

Here is my function inside my service:这是我的服务中的 function:

  async pickerTwoColumns(obj: objectTwoColumns ) {
    this.clearObject(obj.firstColumnOptions);
    this.clearObject(obj.secondColumnOptions);
    let options: PickerOptions = {
      //cssClass: 'picker',
      backdropDismiss: false,
      buttons: [
        // {
        //   text: 'Annuler',
        //   role: 'cancel'
        // },
        {
          text: 'Valider',
          handler: () => {
            return true;
          }
        }
      ],
      columns:[
        {
        name:obj.firstColumnName,
        selectedIndex: obj.firstColumnIndex,
        suffix: obj.firstColumnSuffix,
        options: obj.firstColumnOptions,
        columnWidth: '5',
        suffixWidth: '1',
        prefixWidth: '10',


      },
      {
        name:obj.secondColumnName,
        selectedIndex: obj.secondColumnIndex,
        suffix: obj.secondColumnSuffix,
        options: obj.secondColumnOptions,
        columnWidth: '10',
        suffixWidth: '5',
        prefixWidth: '100',
      },
    ]
    };

    let picker = await this.pickerController.create(options);
    picker.columns[0].options.forEach(element => {
      delete element.selected;
      delete element.duration;
      delete element.transform;
    });

    // https://github.com/ionic-team/ionic-framework/issues/17664
    picker.present();
    picker.onDidDismiss().then(async data => {
      let firstColumn = await picker.getColumn(obj.firstColumnName);
      let secondColumn = await picker.getColumn(obj.secondColumnName);
      obj.firstColumnTextValue = firstColumn.options[firstColumn.selectedIndex].text;
      obj.secondColumnTextValue  = secondColumn.options[secondColumn.selectedIndex].text;
      
      obj.firstColumnIndex = firstColumn.selectedIndex;
      obj.secondColumnIndex = secondColumn.selectedIndex;

      //this.checkIfValidateOk();
      
    })
    
    console.log(obj);
    
  }

So when the user click "Valider", I'd like the function to at least true and the best should be to return my object obj, so when it's click I can assign the value to another variable in my main page when I use the function like that:因此,当用户单击“Valider”时,我希望 function 至少为真,最好的方法是返回我的 object obj,因此当单击时,我可以在使用function 像这样:

  async sizePicker() {

    await this.helpService.pickerTwoColumns(this.sizePickerObject).catch(resData => {
      console.log(resData);
    })
  }

But nothing happen in my main code, after clicking "Valider", actually I got the console.log return undefined when I open the function sizePicker whereas I'd like to have something happen when the "Valider" button is pressed...但是在我的主代码中没有任何反应,在单击“Valider”之后,实际上当我打开 function sizePicker时我得到了console.log返回 undefined 而我想在按下"Valider"按钮时发生一些事情......

Thanks for your help.谢谢你的帮助。

You can use Promises with async like so.您可以像这样将Promises与 async 一起使用。 I can't execute your code since I don't have the complete Class, but this should give you the idea.我无法执行你的代码,因为我没有完整的 Class,但这应该会给你这个想法。

Note : I didn't modify the validator method, but you can use the approach I outlined below to do validation work in the handler and resolve or throw an error accordingly.注意:我没有修改验证器方法,但是您可以使用我在下面概述的方法在处理程序中进行验证工作并相应地解决或引发错误。

First thing to do is return a Promise in the pickerTwoColumns:首先要做的是在 pickerTwoColumns 中返回 Promise:

    async pickerTwoColumns(obj: objectTwoColumns ) {
        return new Promise(resolve => {
       
            this.clearObject(obj.firstColumnOptions);
            this.clearObject(obj.secondColumnOptions);

You will then want to resolve the promise in the picker onDidDismiss method --> look for resolve(obj) call然后,您将希望在选择器 onDidDismiss 方法中解析 promise --> 查找 resolve(obj) 调用


    picker.onDidDismiss()
        .then(async data => {
            let firstColumn = await picker.getColumn(obj.firstColumnName);
            let secondColumn = await picker.getColumn(obj.secondColumnName);
            obj.firstColumnTextValue = firstColumn.options[firstColumn.selectedIndex].text;
            obj.secondColumnTextValue  = secondColumn.options[secondColumn.selectedIndex].text;
            
            obj.firstColumnIndex = firstColumn.selectedIndex;
            obj.secondColumnIndex = secondColumn.selectedIndex;
    
            //this.checkIfValidateOk();
            
            resolve(obj);
        })
        .catch((error)=> {
            //handle error
        })

Finally, I would use this pattern in sizePicker method too.最后,我也会在sizePicker方法中使用这种模式。 You should resolve the data in the.then method and handle exceptions in.catch您应该在.then 方法中解析数据并在.catch 中处理异常

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

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