Cannot read property 'post' of undefined vue

落爺英雄遲暮 提交于 2019-12-01 23:43:19

First of all, thanks to @Bert who was patient and helped me find the solution

In my main.js, i changed this line

var VueResource = require('vue-resource');

for

import VueResource from "vue-resource"

and use

Vue.use(VueResource);

Then, in my app vue component i changed

Vue.http.post

For

this.$http.post

That way we fixed the error!

You were using export default script, therefore your this keyword, does not have access to the Vue instance original module, but to an imported object component. The most likely scenario is that with require, vue-resource is only available to that original module, so using import VueResource from "vue-resource", with the es6 syntax the vue-resource library is hoisted with an internal module, before it is evaluated and cached on the browser alongside the Vue instance. In some cases people don't use arrow functions and get out of scope. Another alternative would be to use the Vue instance itself instead of exporting a default object. Check out http://voidcanvas.com/import-vs-require/ you can probably get more out of it than i did.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!