我在SO上发表了这个答案:Is there a proper way of resetting a component's initial data in vuejs?
但是,现在不允许使用当前方法,VueJS禁止更改$ data var。
正如您在此https://github.com/vuejs/vue/issues/2873中所见($ data不允许修改。)
因此,如果我尝试上述方法,我会收到VueJS警告:
[Vue警告]:避免替换实例根$ data。使用嵌套数据 属性而不是。
这是我的JS代码,
function initialState () {
return {
h2: 0,
ch4: 0,
c2h6: 0,
c2h4: 0,
c2h2: 0,
c3h8: 0,
c3h6: 0,
co: 0,
co2: 0,
o2: 0,
n2: 0,
selected: ''
}
}
export default {
name: 'something',
data () {
return initialState() // This is working fine
},
computed: {
tdcg: function () {
// some logic here...
}
},
methods: {
resetFields: function () {
this.$data = initialState() // --> This is what I want to achieve!
}
}
}
那么重新初始化数据的正确和最简单的方法是什么?
答案 0 :(得分:38)
您可以使用Object.assign
遍历所有属性并分配它们:
export default {
data () {
return {
h2: 0,
// other attributes...
};
},
methods: {
resetFields () {
Object.assign(this.$data, this.$options.data.call(this));
}
}
}
这是一个演示小提琴:https://jsfiddle.net/797yyvtz/
注意:我正在使用this.$options.data
再次调用原始data
方法来获取数据的新副本。无需单独的initialState
功能。 data
方法是初始状态函数。
答案 1 :(得分:2)
您是否尝试迭代initialState对象并再次设置它?以下是示例代码:
function initialState() {
return {
h2: 0,
ch4: 0,
// and so on... finally
selected: ''
}
}
export default {
name: 'something',
data: function() {
return initialState()
},
computed: {
// ...
},
methods: {
resetFields: function() {
// Fetch the initialState object locally, so we do not have to call the function again
let initialData = initialState();
// Iterate through the props
for (let prop in initialData) {
// Reset the prop locally.
this[prop] = initialData[prop];
}
}
}
}
在我当地的有限实验中,它似乎确实有效。让我知道你对这种方法的看法。
答案 2 :(得分:1)
使用名为" data"的密钥将所有数据包装到dict中。或其他的东西。然后你可以通过设置this.data = {xx:yy}重新初始化整个数据,或直接更改一个数据项,如this.data.h2 = 2.
function initialState () {
return {
h2: 0,
ch4: 0,
c2h6: 0,
c2h4: 0,
c2h2: 0,
c3h8: 0,
c3h6: 0,
co: 0,
co2: 0,
o2: 0,
n2: 0,
selected: ''
}
}
export default {
name: 'something',
data () {
return {data: initialState()} // This is working fine
},
computed: {
tdcg: function () {
// some logic here...
}
},
methods: {
resetFields: function () {
this.data = initialState() // --> This is what I want to achieve!
}
}
}
答案 3 :(得分:1)
你可以试试这个:
export default {
// Initialize your data
data() {
return {
// Initialize the form field (STEP 1)
formFields: {
name: '',
email: '',
password: '',
moreData: {
field1: '',
field2: [],
},
},
// Create an object property used for cloning (STEP 2)
formFieldsCopy: {},
};
},
// When the DOM is mounted copy the
// formField you want to a temporary field
// You can use lodash ._clone or ES6 spread operator (STEP 3)
mounted() {
this.formFieldsCopy = { ...this.formFields
};
},
methods: {
// Write the function to reset the form (STEP 4)
resetFormFields() {
this.formFields = { ...this.formFieldsCopy
};
},
submit() {
// Do you normal Axios requests here
// and call you reset function. (STEP 5).
this.resetFormFields();
},
},
};

答案 4 :(得分:1)
我的解决方案:
mounted(){
this.saveData() // you can load if you need previous data
},
methods: {
saveData(){
localStorage.setItem(this.$options.name,JSON.stringify(this.$data));
},
loadData(){
if (localStorage.getItem(this.$options.name) !== null) {
let data= JSON.parse(localStorage.getItem(this.$options.name));
Object.keys(data).forEach((key)=>{this[key] = data[key];});
}
}
}
如果需要,您可以再次加载或保存