A
A
Anna Shatkovskaya2018-10-23 09:55:53
JavaScript
Anna Shatkovskaya, 2018-10-23 09:55:53

Why does Vue get the "Cannot read property '$http' of undefined" error?

When refreshing the page in the browser or using F5, an error pops up:
5bcec5ad2e95a076082473.png
What is the problem, please help me figure it out.
Code in app.vue:

import { GET_WAYBILLS } from './store/actions/waybills.js'
import { mapGetters } from 'vuex'

  export default {
    name: 'App',
    components: {
      Navigation,
    },
    computed: {
      ...mapGetters(['isAuthenticated', 'isMenu', 'urlServer', 'waybills']),

    },
    created() {
      this.getWaybills()
    },
    watch: {
      '$route': 'getWaybills'
    },
    methods: {
      getWaybills() {
        if (this.isAuthenticated && (this.urlServer != '') && (this.waybills == '')) {
          this.$store.dispatch(GET_WAYBILLS).then(() => {
            this.$router.push('/')
          })
        }
      }
    }
  }

Code in main.js:
import VueResource from 'vue-resource'
Vue.use(VueResource)

Code in Vuex module:
import mainVue from '../../main.js'
const actions = {
  [GET_WAYBILLS]: ({
                     commit,
                     rootGetters
                   }) => {
    return new Promise((resolve, reject) => {
      mainVue.$http.get(rootGetters.urlServer+'waybills', {
        headers: {
          'Content-Type': 'application/json',
          'x-api-token': localStorage.getItem('user-token'),
        },
      }).then(response => {
        commit(GET_WAYBILLS, response.body)
        resolve(response.body)
      }, response => {
        reject(response)
      })
    })
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anna Shatkovskaya, 2018-10-30
@Miki8887

Solution:
Code in Vuex module:

import Vue from 'vue' 
...
 Vue.http.post

S
shmatuan, 2018-10-23
@shmatuan

Is that really all the code in main.js ?

import VueResource from 'vue-resource'
Vue.use(VueResource)

judging by the error - it simply does not return anything, so main.js should have Or use viaimport mainVue from '../../main.js'export default Vue
this.$http

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question