【问题标题】:Vue-resource issue with $http - Uncaught TypeError: Cannot read property 'post' of undefined$http 的 Vue 资源问题 - 未捕获的类型错误:无法读取未定义的属性“帖子”
【发布时间】:2016-12-02 17:33:13
【问题描述】:

我是 Vue 新手,我在 Laravel 5.3 项目中使用它。

在我的 app.js 文件中,我有以下内容

require('./bootstrap');

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

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

然后在我的引导文件中我有

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

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

  next();
});

在我的 CheckoutForm.vue 文件中,我的模板和 js 如下:

<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">
        {{ plan.name }} - ${{ plan.price / 100}}
      </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: admin.stripeKey,
        image: "https://stripe.com/img/documentation/checkout/marketplace.png",
        locale: "auto",
        panelLabel: "Subscribe for: ",
        email: admin.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);
    }

  }

};

我遇到的问题是我使用 this.$http.post() 提交表单的调用给了我错误

未捕获的类型错误:无法读取未定义的属性“帖子”

我认为这是加载 vue-resource 的问题。

我检查了我的 package.json 文件有 vue-resource 并且我已经通过 npm 安装了它,但仍然存在同样的问题。

任何帮助或想法将不胜感激。

【问题讨论】:

  • 你需要告诉Vue vue-resource。在引导文件中将 require('vue-resource') 替换为 Vue.use(require('vue-resource'))
  • 嗨,Donkarnash,我试过了,但我仍然遇到同样的错误:Uncaught TypeError: Cannot read property 'post' of undefined at TokenCallback.token [as fn] (eval at (app. js:84), :52:21) at TokenCallback.trigger (checkout.js:3) at TokenCallback.trigger (checkout.js:3) at IframeView.onToken (checkout.js:3) at IframeView.close (checkout.js:3) 在 Object.close (checkout.js:3) 在 RPC.processMessage (checkout.js:2) 在 RPC.processMessage (checkout.js:2) 在 RPC.message (checkout.js:2 ) 在 checkout.js:2
  • 该错误似乎是由 Stripe 的 checkout.js 触发的。检查 app.js 中的第 84 行并尝试在该行之前 console.log(this.$http) 以确保它不是 Vue 错误。

标签: php laravel-5 vue-resource


【解决方案1】:

好的,问题在于脚本部分中的 CheckoutForm.vue 文件 - 在 created() method token:function(token){} this 中没有引用 Vue 实例,因此 this.$http 未定义。

将其更改为使用 ES2015 箭头语法,如下面代码中修改的那样,那么您不应将 this.$http 视为未定义。

或者您需要在匿名函数中绑定this 以获得created() 内的令牌。

<script>
export default {

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

  created(){
    this.stripe = StripeCheckout.configure({
    key: admin.stripeKey,
    image: "https://stripe.com/img/documentation/checkout/marketplace.png",
    locale: "auto",
    panelLabel: "Subscribe for: ",
    email: admin.user.email,
    //here you are using function(token) within it the `this` will not reference the Vue instance, hence this.$http is undefined.
    //token: function(token){
    //so change it to use the ES2015 arrow syntax
    token:(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);
    }

  }

};

【讨论】:

  • 你好唐卡纳什。是的,这就是问题所在,现在工作正常。感谢您的帮助
  • 很高兴我能帮上忙。如果答案解决了您的问题,您可以将其标记为其他访问此问题的人知道的答案。快乐编码..
猜你喜欢
  • 1970-01-01
  • 2017-08-23
  • 2014-08-31
  • 1970-01-01
  • 2020-09-18
  • 1970-01-01
  • 2021-05-03
  • 2021-04-03
  • 2020-09-19
相关资源
最近更新 更多