Uncaught TypeError: Cannot read property 'post' of undefined
While going thru the tutorial for payments with Laravel and stripe I am getting the error above when trying to submit the form in the Vue template. Below is the template and the line this.$http.post('payments/checkout', this.$data) .then(response => alert('Thank you for your purchase!.')); is where I get the error. I have checked that Vue and vue-resource are installed and they seem to be. Any ideas will be appreciated it.
/**
* We'll load jQuery and the Bootstrap jQuery plugin which provides support
* for JavaScript based Bootstrap features such as modals and tabs. This
* code may be modified to fit the specific needs of your application.
*/
window.$ = window.jQuery = require('jquery');
require('bootstrap-sass');
/**
* Vue is a modern JavaScript library for building interactive web interfaces
* using reactive data binding and reusable components. Vue's API is clean
* and simple, leaving you to focus on building your next great project.
*/
window.Vue = require('vue');
Vue.use(require('vue-resource'));
/**
* We'll register a HTTP interceptor to attach the "CSRF" header to each of
* the outgoing requests issued by this application. The CSRF middleware
* included with Laravel will automatically verify the header's value.
*/
Vue.http.interceptors.push((request, next) => {
request.headers.set('X-CSRF-TOKEN', SIGadmin.csrfToken);
next();
});
I removed JQuery and bootstrap and changed the line
Vue.use(require('vue-resource'));
to just
require('vue-resource');
and double checked that the package.json file has the vue-resource loaded and ran npm install as well as composer update.
However, I am still getting the same error as if vue-resource is not loaded at all. I also try to create a get request and did not work either, making me certain that the issue is with the load of vue-resource.
if anyone has any ideas I will appreciate the help
@prashant.godhwani You are 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 you should use import VueResource from "vue-resource", this way, with the es6 syntax the vue-resource library is hoisted with an internal module, before it is evaluated and cached on the browser with the alongside the Vue instance@jurjen has stated a similar constraint, where the this keyword is not pointing at Vue.
Another alternative would be to use the Vue instance itself instead of exportin a default object.
Check out http://voidcanvas.com/import-vs-require/ you can probably get more out of it than i did.