【问题标题】:How can I re-initialize (only) partial of data values in vue?如何在 vue 中重新初始化(仅)部分数据值?
【发布时间】:2017-12-20 15:38:02
【问题描述】:

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

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()
    }
},
... 

有没有办法做到这一点?

【问题讨论】:

  • 你的语法正确吗? data: { return { 正确吗?不是data: function () { return {吗?
  • 是的,你是对的 @acdcjunior ,实际上是 ...data(){... 现在会修复它。

标签: javascript ecmascript-6 vue.js


【解决方案1】:

试试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的对象),返回target 对象

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2019-12-05
    • 1970-01-01
    相关资源
    最近更新 更多