gpt4 book ai didi

laravel - 避免在运行时向 Vue 实例或其根 $data 添加响应式属性 - 在 data 选项中预先声明它。

转载 作者:搜寻专家 更新时间:2023-10-30 22:16:20 25 4
gpt4 key购买 nike

我对使用 VueJS2 有点困惑。我向数据容器添加了一些变量,以便将其发送到我的 API。这工作正常,但 Vue 向我抛出一条警告/错误消息,我不知道如何解决:

Avoid adding reactive properties to a Vue instance or its root $data at runtime - declare it upfront in the data option.

var app = new Vue({
el: '#app',
data: {
incidentReference: '',
streetName: '',
latitude: '',
longitude: '',
featureTypeId: 1,
archived: 0
},

computed: {
href() {
return '#' + this.name.toLowerCase().replace(/ /g, '-');
}
},

mounted: function () {
this.getIncidents();
},

methods: {
onSubmit() {
axios.post('/api/v1/incidents', this.$data)
.then(response => alert('Success'))
.catch(error => {
console.log(error.response);
})
},

getIncidents: function() {
console.log('getIncidents');
var self = this;
axios.get('/api/v1/incidents').then(function(response) {
// set data on vm
console.log(response.data);
var incidentsReceived = response.data.data.map(function (incident) {
return incident;
});
Vue.set(self, 'incidents', incidentsReceived);
});
}
}
});

最佳答案

您正在为 API 的响应创建一个新的响应式属性

Vue.set(self, 'incidents', incidentsReceived);

不确定您是否拼错了属性名称或忘记创建该属性。只需在 data 部分使用现有属性

Vue.set(self, 'incidentReference', incidentsReceived); //change property name

data: {
incidents: null, //or create this property
},

关于laravel - 避免在运行时向 Vue 实例或其根 $data 添加响应式属性 - 在 data 选项中预先声明它。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42096009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com