gpt4 book ai didi

javascript - 如何重新初始化(仅)vue 中的部分数据值?

转载 作者:行者123 更新时间:2023-12-03 02:50:05 25 4
gpt4 key购买 nike

我知道我们可以像这样重新初始化数据:

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() 返回的对象)复制到 target(在本例中为具有 is_activeis_collapsed 的对象),返回目标对象

关于javascript - 如何重新初始化(仅)vue 中的部分数据值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47909626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com