我正在尝试将我的数据变量恢复到原始状态,如下所示:
export default
{
data: function ()
{
return {
countries: [],
name: '',
summary: '',
salarytype: '',
salaryfrom: '',
salaryto: '',
location: '',
contactemail: '',
contactphone: '',
errors: new Errors()
}
},
methods:
{
addJob: function()
{
axios.post('/jobs/create', this.$data)
.then(response => {
if(response.data.status === true){
resetFields();
$('#createJob').modal('hide');
getJobTable();
}
else{
formError = response.data.message;
}
})
.catch(error => this.errors.record(error.data))
},
resetFields: function() {
Object.assign(this.$data, this.$options.data.call(this));
}
},
mounted: function()
{
axios.get('/countries')
.then(response => {
this.countries = response.data;
})
}
}
当我这样做时,我得到一个错误说:
app.js:34533 TypeError: Cannot read property 'name' of undefined
at Errors.get (app.js:12209)
at Proxy.render (app.js:33512)
at VueComponent.Vue._render (app.js:36314)
at VueComponent.updateComponent (app.js:36707)
at Watcher.get (app.js:37032)
at Watcher.run (app.js:37101)
at flushSchedulerQueue (app.js:36905)
at Array.<anonymous> (app.js:34571)
at nextTickHandler (app.js:34520)
at <anonymous>
我已经检查了堆栈以重置数据值,并且它建议按照我上面的方式进行操作,但这会引发错误。我该如何解决这个问题,以便重置数据?
答案 0 :(得分:0)
if(response.data.status === true){
resetFields();
应该是
if(response.data.status === true){
this.resetFields();
如果要按该名称运行组件方法。
你是否也有一个在组件定义之外定义的名称的函数?或者这是你的例子中的拼写错误?