search

Home  >  Q&A  >  body text

javascript - vue2.0 subcomponent uses $parent.$emit and $on to communicate with the parent component and reports an error?


This is a subcomponent

This is the parent component

Since vue2.0 has abandoned the events attribute, how do click events in my child component bubble up to the parent component, and when should the parent component listen to this event and trigger it.
I try to pass this.$parent.$emit('function','value') under the click event of the child component,
Then pass this.$on("click" in the created hook of the parent component ,function(value){...}),
The method in methods function(value){console.log(value);}
But when an error is reported on the page, the function can be realized, but the data does not change. , so I’m asking for help from a master who has used vue2.0. I’ve been stuck here for a long time.

仅有的幸福仅有的幸福2745 days ago757

reply all(1)I'll reply

  • 为情所困

    为情所困2017-05-19 10:34:24

    vue2
    Use $on(eventName) to listen for events $on(eventName) 监听事件
    使用 $emit(eventNameUse $emit(eventName) to trigger events
    For example:
    Subcomponent

    methods: {
         this.$emit('fn', 'value')
    }

    Parent component

    <ratingselect v-on:fn="type">
    
    </ratingselect>

    reply
    0
  • Cancelreply