javascript - VUE 组件中的 undefined variable

标签 javascript vue.js fullcalendar

出于某种原因,我可以在我的方法部分访问 this.id,但不能在我的数据部分访问。我已经阅读了一些关于 this 关键字细微差别的帖子,但无法弄清楚为什么 this.id 在我的数据部分中是 undefined 。这是代码:

calendar.vue(重要部分):

export default {
   name: 'calendar',
   data() {
      return {    
         eventSources: [
         // your event source
            {
               url: 'http://localhost:3000/getAppointments',
               type: 'GET',
               data: {     
                   id: this.id //this.id is undefined
               },
            }
         ],
         selected: {},
         val: [],
         id: this.$store.getters.user,
      };
   },
   methods: {
      getApts: function(){
         this.$http.get('http://localhost:3000/getAppointments?id='+this.id)
      //here this.id is defined 
      }
   }
};

最佳答案

当调用数据函数来初始化你的 Vue 时,在数据函数内部,this 将绑定(bind)到 Vue.在上面的代码中,没有 Vue 的 id 属性,因为 Vue 正在附加您定义的所有数据属性。这就是为什么 this.idundefined 的原因。

我建议你只使用

  data() {
    return {    
      eventSources: [
        // your event source
        {
          url: 'http://localhost:3000/getAppointments',
          type: 'GET',
          data: {     
            id: this.$store.getters.user
          },
        }
      ],
      selected: {},
      val: [],
      id: this.$store.getters.user,
    };
  },

关于javascript - VUE 组件中的 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45060602/

相关文章:

javascript - Vue.js 的 if 语句存在问题。工作起来真的很奇怪

javascript - Jquery 插件与谷歌日历

javascript - fullcalendar 事件点击与事件内的链接

javascript - 在 vue.js 中创建一个包含数据的 obj 名称

javascript - 当滚动到页面末尾时更新...但不断更新

drag-and-drop - 转换到某个事件(而不是日历) - 如何识别事件?

JavaScript:特殊变量/参数?

javascript - 来自 json 文件的 D3.js 多力布局图

javascript - 哪个更好 : public javascript CDN or self-hosted combined javascript?

javascript - 为什么我尝试设置带有值的 JSON 字段时会出现此错误?未捕获的类型错误 : Cannot set property 'today_avg_price' of undefined