[英]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.