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

避免向Vue实例或其根$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);
            });
        }
    }
});

推荐答案

您正在API的响应上创建一个新的被动属性

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

不确定您是否拼错了属性的名称或忘记创建该属性.只需使用现有的属性就可以了

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

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

Vue.js相关问答推荐

如何禁用Vuetify v—stepper操作按钮

使用nuxt I18N验证规则消息翻译

VITE:无法为VUE单文件组件加载URL

使用 vue / nuxt js 切换 fontawesome 图标

在 Vue 中启用(控制台)记录路由事件

在组件上的 Vue 3 中获取 URL 查询参数

在组件的样式部分使用 Vue 变量

Vue.js 过滤数组

v-for 中的 VueJS 插槽

JavaScript/VueJS:判断数组是否包含具有特定值元素的对象

我可以在 Vue 实例方法中传播的 mapMutations 中使用this吗?

使用 vue-router 的默认子路由

Nuxt.js - API 调用的最佳场所

如何在两个属性上使用 Vue/Vuetify 自动完成过滤器?

根据路由动态插入 CSS 类到导航栏

如何从 vue 组件调用 App.vue 中的方法

从 ajax 调用填充 Vue.js 中的下拉列表

vue-router如何持久化导航栏?

在 Chrome DevTools 中实时更改时 destruct 了 Vue.js 应用程序(Webpack 模板)的页面样式

根据nuxt中的url参数动态加载组件