简体   繁体   English

召唤伟大的孩子组成方法

[英]Call great grand children component method

I got a set of nested components as follows in Vue 2.2.1: 我在Vue 2.2.1中得到了一组嵌套组件:

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

And I need to call a method getToken on the Stripe component when the form is submitted. 我需要在提交表单时在Stripe组件上调用方法getToken On my <VForm> component I have the following template. 在我的<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>

But I get Uncaught TypeError: Cannot read property 'getToken' of undefined . 但我得到Uncaught TypeError: Cannot read property 'getToken' of undefined I also tried it by emitting an event at the <v-form> level but, if i'm not mistaken, it is my understanding that the events flow from child to parent, so that didn't work. 我也通过在<v-form>级别发出一个事件来尝试它,但是,如果我没有弄错,我的理解是事件从子节点流向父节点,因此不起作用。

How can I trigger stripe.getToken on <v-form> submit? 如何在<v-form>提交时触发stripe.getToken

You could use a bus. 你可以用一辆公共汽车。

const bus = new Vue();

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM