javascript - Vue3中API的对象解构
问题描述
我想将数据从 API 放到 Vue 状态。什么是快速的方法来做到这一点?
getProjectData(id) {
axios.get(`/api/project/${id}`).then((res) => {
console.log(res)
/* I don't want to manually assign the value one by one (since the name is the same, there should be a quick way */
this.formState.name = res.data.projects.name
this.formState.stage = res.data.projects.stage
this.formState.status = res.data.projects.status
this.formState.description = res.data.projects.description
this.formState.stations = res.data.projects.stations
});
},
解决方案
您可以使用扩展运算符来实现它:
this.formState = {...res.data.projects}
需要指出的是,这些属性是浅拷贝的。如果属性值是一个对象,对它所做的任何修改都将反映在该res.data.projects
对象中。
推荐阅读
- ios - CloudKit 服务器将推送通知发送到最大值。500 台设备
- java - 如何使我的基于堆栈的计算器成功打印几个连续相加的数字的波兰符号?
- cmake - 强制 CMake 使用绝对包含路径
- javascript - 如何删除三个 js 中的剪裁平面?
- python - 操作列表中、列表中、列表中的每个项目(字符串)
- plugins - Facebook 客户聊天插件问题
- java - 如何使用 Maven、Tycho 和 Jython 构建 Eclipse 插件?
- android - 通知声音不会停止android
- ms-access - VB6 和 MS Access,如果用户为真,则保存时间和日期
- html - 使用 Excel VBA 从网页下载文件