首页 > 解决方案 > fetch() 方法在 Nuxtjs 的组件中使用时抛出错误

问题描述

fetch()在导航组件中使用方法,但是当我运行我的应用程序时,我看到了[Vue warn]: Property or method "menus" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option or for class-based components, by initializing the property.

我在导航中的脚本代码。

export default {
  name: 'AppHeader',
  methods: {
    async fetch ({ $content }) {
      const menus = await $content('menu').fetch()
      return {
        menus
      }
    }
  }
}

我试图在数据中添加菜单数组,但没有运气。

export default {
  name: 'AppHeader',
  data () {
    return {
      menus: []
    }
  },
  methods: {
    async fetch ({ $content }) {
      const menus = await $content('menu').fetch()
      return {
        menus
      }
    }
  }
}

标签: vue.jsvuejs2vue-componentnuxt.jsnuxtjs

解决方案


推荐阅读