最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Cannot read property 'get' of undefined - Vue-resource and Nuxt - Stack Overflow

programmeradmin0浏览0评论

been receiveing error like this in my Vuex Store

TypeError: Cannot read property 'get' of undefined

I already have added a plugin for the vue-resource

This is my code that is receiving that error

async getAllStudents({mit}) {
await Vue.$http.get(`/api/students`)
  .then((res) => {
    if (res.status === 200) {
      mit('setAllStudents', res.data)
    }
  }).catch(function(error) {
    console.log(error);
  });

},

been receiveing error like this in my Vuex Store

TypeError: Cannot read property 'get' of undefined

I already have added a plugin for the vue-resource

This is my code that is receiving that error

async getAllStudents({mit}) {
await Vue.$http.get(`/api/students`)
  .then((res) => {
    if (res.status === 200) {
      mit('setAllStudents', res.data)
    }
  }).catch(function(error) {
    console.log(error);
  });

},

Share Improve this question edited Jun 20, 2020 at 15:00 Boussadjra Brahim 1 asked Jun 20, 2020 at 14:29 Reinard CarrancejaReinard Carranceja 772 silver badges11 bronze badges 2
  • In which file are you doing this? – BeHappy Commented Jun 20, 2020 at 14:37
  • In my store file - /store/enrollment – Reinard Carranceja Commented Jun 20, 2020 at 14:38
Add a ment  | 

2 Answers 2

Reset to default 4

Try to use this instead of Vue and try axios:

async getAllStudents({mit}) {
await this.$axios.get(`/api/students`)
  .then((res) => {
    if (res.status === 200) {
      mit('setAllStudents', res.data)
    }
  }).catch(function(error) {
    console.log(error);
  });
},

make sure that your nuxt.config.js looks like :

const routerBase = process.env.DEPLOY_ENV === 'GH_PAGES' ? {
  router: {
    base: '/boussadjra-brahim/'
  }
} : {}

export default {
  mode: 'universal',
  /*
  ** Headers of the page
  */
  head: {
    titleTemplate: '%s - ' + 'Boussadjra Brahim',
    title: 'Boussadjra Brahim' || '',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: process.env.npm_package_description || '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ],

  },

  /*
  ** Plugins to load before mounting the App
  */
  plugins: [
  ],

  ....
  /*
  ** Nuxt.js modules
  */
  modules: [
    // Doc: https://axios.nuxtjs/usage
    '@nuxtjs/axios',
  ],


  ....
}


Try this, use this._vm instead of Vue:

async getAllStudents({mit}) {
await this._vm.$http.get(`/api/students`)
  .then((res) => {
    if (res.status === 200) {
      mit('setAllStudents', res.data)
    }
  }).catch(function(error) {
    console.log(error);
  });
},
发布评论

评论列表(0)

  1. 暂无评论