在 Quasar (vue) 中使用解析从数据中获取数据 - return 未定义

Using parse in Quasar (vue) to fetch data from data - return undefined

我正在使用 parse.js 从 Quasar SPA 构建中的数据库中提取数据(基于 vue.js)。通过方法调用数据时从数据库中获取数据时,我得到的数据未定义,但是当我将它直接放入创建函数时它起作用了。我猜他们可能在不同的范围内(?)但我不知道为什么。

这个有效:

export default {
  name: "MyPage",
  data() {
    return {
      myData: []
    };
  },

  created: async function() {
    let query = new parse.Query("TheData");
    const results = await query.find();
    this.myData = results;
    console.log(this.myData);
  }

但是当我将数据封装到一个方法中时 returns

TypeError: Cannot set property 'myData' of undefined

export default {
  name: "MyPage",
  data() {
    return {
      myData: []
    };
  },

  created: async function() {
    this.getTheData();
  },
  methods: {
    getTheData: async () => {
      let query = new parse.Query("TheData");
      const results = await query.find();
      this.myData = results; // TypeError: Cannot set property 'myData' of undefined
      console.log(this.myData);
    },

我建议你尝试改变这个

created: async function() {
  this.getTheData();
}

为了

async mounted() {
    this.getTheData();
}

根据文档说:

Don't use arrow functions on an options property or callback, such as created: () => console.log (this.a) or vm. $ watch ('a', newValue => this. myMethod ()). Since an arrow function doesn't have a this, this will be treated as any other variable and lexically looked up through parent scopes until found, often resulting in errors such as Uncaught TypeError: Cannot read property of undefined or Uncaught TypeError: this.myMethod is not a function.

https://vuejs.org/v2/guide/instance.html#Instance-Lifecycle-Hooks