簡體   English   中英

如何在Vue中重新初始化(僅)部分數據值?

[英]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_activeis_collapsed的對象), 並返回target對象

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM