请问一个axios使用的问题,谢谢各位!

[Vue warn]: Error in created hook: "TypeError: Cannot read property 'get' of undefined"

请问我如下这样,为什么发生错误,store里调用不到this.$http的get方法吗?
//main.js

import store from './store'
import axios from 'axios'
import App from './App.vue'
Vue.prototype.$http = axios

//App.vue

created() {
 this.$store.dispatch('allProducts')
 },

//store/index.js

actions: {
    allProducts() {
        this.$http.get(`http://localhost:3000/login`).
        then(response => {console.log(response)}
    }
}
阅读 1.7k
1 个回答

1、axios用法不对,要先实例化一个axios出来;
2、vuex中this指向的并不是vue实例。
看一下axios的文档吧axios

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题