调用曾孙组件方法

Call great grand children component method

我在Vue 2.2.1中得到了一组嵌套组件如下:

<Root>
  <VForm>
    <Accordion>
      <Panel>
        <Stripe ref="stripe">

并且我需要在提交表单时调用 Stripe 组件上的方法 getToken。在我的 <VForm> 组件上,我有以下模板。

<template>
  <form :method="method" :action="action" :class="classes" :autocomplete="autocomplete" @submit.prevent="submit">
    <slot></slot>
  </form>
</template>

<script>

  export default {

    props: {
      method: {
        type: String,
        default: 'POST'
      },
      action: {
        required: true,
        type: String
      },
      classes: {
        type: String
      },
      autocomplete: {
        type: String,
        default: 'on'
      }
    },

    methods: {
      submit(){
        this.$refs.stripe.getToken
      }
    }
  }

</script>

但我得到 Uncaught TypeError: Cannot read property 'getToken' of undefined。我还通过在 <v-form> 级别发出事件进行了尝试,但是,如果我没记错的话,我的理解是事件从子级流向父级,所以这没有用。

如何在 <v-form> 提交时触发 stripe.getToken

你可以坐公共汽车。

const bus = new Vue();

Vue.component("parent", {
    methods:{
        triggerStripe(){
            bus.$emit('get-token');
        }
    }
})

Vue.component("stripe",{
    mounted(){
        bus.$on('get-token', () => this.getToken());
    }
})