我对使用 VueJS2 有点困惑。我向数据容器添加了一些变量,以便将其发送到我的 API。这工作正常,但 Vue 向我抛出了一条警告/错误消息,我不知道如何解决:
避免在运行时向 Vue 实例或其根 $data 添加反应性属性 - 在 data 选项中预先声明。
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);
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
Ald*_*o88 10
您正在为 API 的响应创建一个新的反应性属性
Vue.set(self, 'incidents', incidentsReceived);
Run Code Online (Sandbox Code Playgroud)
不确定您是否拼错了属性的名称或忘记创建该属性。只需使用您data部分的现有属性
Vue.set(self, 'incidentReference', incidentsReceived); //change property name
Run Code Online (Sandbox Code Playgroud)
或者
data: {
incidents: null, //or create this property
},
Run Code Online (Sandbox Code Playgroud)
就我而言,在使用 Jest 进行单元测试期间,我正在设置selected但没有组件,因此出现此错误。
wrapper.setData({
selected: recipients,
});
Run Code Online (Sandbox Code Playgroud)
因此在组件上创建了属性,然后它就可以正常工作了。
| 归档时间: |
|
| 查看次数: |
14835 次 |
| 最近记录: |