我有一个表,我将 userList
作为 props 并使用 v-for="(user , index) in userList":key="index"将其渲染到表中
和字符串 :value="user.username"
但现在我的 swagger API 需要我将其修补为 s.th 的对象,如 username:{value: "",操作数:1}
有谁知道我应该如何更改数据类型并将其作为 formData 发送到我的 axios 补丁?
在我发现我需要一个对象来传递之前,我的 axios.patch 也是这样的:')
async userEdit(id) {
const response = await this.$store.dispatch('axiosPatch', {url: `/employees/${id}`,
formData: {
username: this.username.value,
email: this.email,
mobile: this.mobile,
}
}, this.$data)
},
我的输入表单也是这样的:
<input type="text" class="text-xs text-secondary mb-0 text-center
border-0 bg-white w-100 p-1"
:disabled="!user.disabled"
:value="user.nationalCode">
最佳答案
您可以在数据部分中声明一个对象变量,然后为其分配项目,例如我们在数据中定义了 items
对象,然后我们有 items.username
,items.email
等等,最后当你想将此对象发送到服务器时,你可以通过对其进行 for 循环来转换所有这些。例如:
let dataToPatch = [];
let items = {username:'Ali',email:'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="532736202713273620277d303c3e" rel="noreferrer noopener nofollow">[email protected]</a>'};
for(item in items){
const newItem = {};
newItem[item] ={value:items[item],operand: 1};
dataToPatch.push(newItem);
}
console.log(dataToPatch)
关于javascript - axios 从同一输入 vue 3 获取字符串和补丁作为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72751309/