javascript - How to pass parameters from sub-component to parent component in Vue
给我你的怀抱
给我你的怀抱 2017-05-19 10:20:59
0
8
814
  • Background: Vue ES6, the child component is called after the parent component is created, and the event trigger onclick has been encapsulated in the underlying component (the child component of the child component)

  • Question: How does a child component pass parameters to the parent component when there is no event binding in the parent component?

给我你的怀抱
给我你的怀抱

reply all(8)
仅有的幸福
  1. EventBus (a situation without event binding that does not quite meet the requirements of the question)

  2. Vuex

我想大声告诉你

dispatch

習慣沉默

Father-child component communication

阿神

$emit()

滿天的星座

The child component uses v-on to listen to an event, and then when it is triggered, it sends the event, which is $emit. Then the parent component also uses v-on to listen to the event you send, and then executes the event defined by your parent component.

伊谢尔伦

The person above has made it clear, I’m here to make up for it
child.vue

<template>
    <p id="test">向父传递</p>
</template>
<script>
    export default {
        methods: {
            $('#test').click(()=>{
                this.$emit('data', '这是我要传的参数');
            })
        }
    }
</script>

parent.vue

<template>
    <child @data="fnSS"></child>
</template>
<script>
    import child from './child';
    export default {
        components: {
            tagInput
        }
        methods: {
            fnSS(value) {
                alert(value);
            },
        }
    }
</script>
仅有的幸福

$emit()

迷茫

Subcomponent this.$emit("event",data)

Parent component @event

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template