javascript - javascript将对象解构为“this”
问题描述
我想将 response.data 的分页结果分配给 Vue 数据
,所以显而易见的方法很简单
this.data = response.data.data
this.total = response.data.total
this.perPage = response.data.per_page
但是有没有一种 ES6 方法可以将结果直接分配给这样的东西?
const page = { data, total, per_page : perPage } = response.data
this = { ...this, ...page }
注意:上面的代码不起作用。
解决方案
不带this
-this
不能重新分配。
如果data
对象仅包含这些属性,并且您将 的per_page
属性更改response.data
为perPage
,那么您可以使用Object.assign
:
Object.assign(this, response.data);
如果data
对象包含其他属性,或者您无法重命名per_page
,则:
const { data, total, per_page : perPage } = response.data
Object.assign(this, { data, total, perPage });
推荐阅读
- strapi - Strapi mongodd 人口减少
- javascript - StocksController#search 中的 ActionController::UnknownFormat
- sharepoint - SharePoint 是/否字段未从 Power Automate 更新
- c# - 为什么 IEnumerable 的 Where 将第一个参数作为“this”传递
- linux - < 是什么意思
> - android - 如何从 NativeScript-Angular 应用程序中的 android 后台服务访问服务和数据?
- command-line-interface - ls /dev/disk/by-id/ - 如何知道哪个磁盘是哪个?- Proxmox
- ios - 领域将 List<> 对象添加到已经存在的对象
- c# - 从文档中选择单个 XML 节点
- c++ - C++ - 使用泰勒级数近似估计 cos(x)