[英]How can I re-initialize (only) partial of data values in vue?
我知道我们可以像这样重新初始化数据:
function initialData() {
return {
is_active: true,
is_collapsed: true,
resetable_data: 'value',
resetable_stat: 4
}
}
export default {
...
data() {
return {
initialData()
}
},
...
但是我想知道如何只初始化一部分数据。 我的意思是:
function initialData() {
return {
resetable_data: 'value',
resetable_stat: 4
}
}
export default {
...
data() {
return {
is_active: true,
is_collapsed: true,
initialData()
}
},
...
有没有办法做到这一点?
试试Object.assign()
:
function initialData() {
return {
resetable_data: 'value',
resetable_stat: 4
}
}
export default {
...
data() {
return Object.assign(
{
is_active: true,
is_collapsed: true,
},
initialData()
);
},
...
Object.assign(target, ...sources)
将...sources
的属性(在本例中为initialData()
返回的对象Object.assign(target, ...sources)
复制到target
(在本例中为is_active
和is_collapsed
的对象), 并返回target
对象 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.