vendredi 2 décembre 2016

Vue-resource issue with $http - Uncaught TypeError: Cannot read property 'post' of undefined

I am new to Vue and I am using it in a Laravel 5.3 project.

in my app.js file I have the following

require('./bootstrap');

Vue.component('CheckoutForm', require('./components/CheckoutForm.vue'));

const app = new Vue({
  el: '#app'
});

Then in my bootstrap file I have

window.Vue = require('vue');
require('vue-resource');

Vue.http.interceptors.push((request, next) => {
  request.headers.set('X-CSRF-TOKEN', SIGadmin.csrfToken);

  next();
});

In my CheckoutForm.vue file I have the template and the js as follows:

<template>
 <form class="form-horizontal" role="form" method="POST"     action="payments/checkout">
 <input type="hidden" name="stripeToken" v-model="stripeToken" />
 <input type="hidden" name="stripeEmail" v-model="stripeEmail" />

<div class="form-group">
  <div class="col-md-8">
    <label> Select New Plan</label>
    <select name="plan" v-model="plan" class="form-control col-md-8" >
      <option v-for="plan in plans" :value="plan.id">
         - $
      </option>
    </select>
  </div>
</div>
<div class="form-group">
  <div class="col-md-8">
    <button type="submit" class="btn btn-primary" @click.prevent="buy">Proceed to Payment</button>
    <a class="btn btn-default" href="/myaccount">Continue as Trial</a>
  </div>
</div>

<script>
export default {

  props: ['plans'],
  data() {
      return{
        stripeEmail: '',
        stripeToken: '',
        plan: 3,
        status: false
      };
  },

  created(){
    this.stripe = StripeCheckout.configure({
        key: SIGadmin.stripeKey,
        image: "http://ift.tt/1HOsSjY",
        locale: "auto",
        panelLabel: "Subscribe for: ",
        email: SIGadmin.user.email,
        token: function(token){
          this.stripeEmail = token.email;
          this.stripeToken = token.id;

          //this.$el.submit();
          this.$http.post('payments/checkout', this.$data)
              .then(
                response => alert('Thank you for your purchase!.'),
                response => this.status = response.body.status
              );

        }
      });
  },

  methods: {

    buy(){

      let plan = this.findPlanById(this.plan);

      this.stripe.open({
        name: plan.name,
        description: plan.description,
        zipCode: true,
        amount: plan.price
      });
    },

    findPlanById(id){
      return this.plans.find(plan => plan.id == id);
    }

  }

};

The issue I am having is that my call to submit the form with this.$http.post() is giving me the error

Uncaught TypeError: Cannot read property 'post' of undefined

And I think it is an issue with loading vue-resource.

I have checked that my package.json file has vue-resource and I have installed it via npm but still the same issue is present.

Any help or ideas will be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire