猿问

如何将参数传递给await函数

在我的 vue.js 应用程序中,我试图读取name我传递给我的函数的值。我相信这是一个范围问题。环顾四周,我认为这可以用胖箭头函数解决?


这是我用来处理更改事件的内容。有没有办法


async handleChange(event, name) {

    console.log('name: ', name);  // works

    console.log('value: ', event.value);  // works


    try {

        let response = await axios.patch(`/my/path`, {

            name: event.value,  // need to get the value from name

         });


         if (response.status === 200) {

             //

         } else {

             console.error('Error: could not update. ', response);

         }

     } catch (error) {

         console.error('Error: sending patch request. ', error);

     }

}

我也试过这个开始:


handleChange: async (event, name) => {

    ...

}

我只是不确定如何在 axios 补丁中使用粗箭头函数。感谢您的任何建议!


慕雪6442864
浏览 227回答 1
1回答

慕田峪7331174

不完全清楚,但我猜您想将名称作为对象内部的键传递。如果是这样,请执行以下操作:let response = await axios.patch(`/my/path`, {   [name]: event.value,});
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答