简体   繁体   English

VueJS2 属性未定义

[英]VueJS2 property not defined

I wrote the following code and Vue complains:我写了以下代码并且 Vue 抱怨:

[Vue warn]: Property or method "incidents" is not defined on the instance but referenced during render. [Vue 警告]:属性或方法“事件”未在实例上定义,但在渲染期间被引用。 Make sure to declare reactive data properties in the data option.确保在 data 选项中声明反应数据属性。

I don't see why incidents cannot be accessed?我不明白为什么无法访问事件?

var app = new Vue({
    el: '#app',
    data: {
        responders: [],
        incidents: []
    },

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

    methods: {
        getIncidents: function() {
            console.log('getIncidents');
            var app = this;
            this.$http.get('/api/v1/incidents').then(function(response) {
                // set data
                var incidentsReceived = response.data.map(function (incident) {
                    return incident;
                });
                Vue.set(app, 'incidents', incidentsReceived);
            })
        },

        getResponders: function() {
            console.log('fetchResponders');
            var app = this;
            this.$http.get('/api/v1/responders').then(function(response) {
                // set data on vm
                var respondersReceived = response.data.map(function (responder) {
                    return responder
                });
                Vue.set(app, 'responders', respondersReceived);
            });
        }
    }
})

data is meant for internal component data modeling, while props, which can be assigned externally, are defined using the props key for your component. data用于内部组件数据建模,而可以在外部分配的 props 是使用组件的props键定义的。

In other words, try:换句话说,尝试:

var app = new Vue({
  ...,
  props: {
    incidents: {
      type: Array,
      required: false //change this as you see fit.
    }
  },
  ...
});

For full documentation on component properties, please refer to the official guide .有关组件属性的完整文档,请参阅官方指南

EDIT: Didn't read the code very well for the first time.编辑:第一次没有很好地阅读代码。 Verify that you have data inside the response and if not don't set it the incidents array.验证您在响应中是否有数据,如果没有,请不要将其设置为事件数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM