Vue2.0 # $ emit, $ on usage details,
In vue1.0, vm. $ dispatch and vm. $ broadcast are discarded. use $ emit instead. $ on
vm.$on( event, callback )
Listen to custom events on the current instance. Events can be triggered by vm. $ emit. The callback function receives additional parameters for all input event-triggered functions.
vm.$emit( event, […args] )
Trigger events on the current instance. All additional parameters are passed to the listener for callback.
Example:
// Parent component <template> <ratingselect @ select-type = "onSelectType"> </ratingselect> </template> <script> data () {return {selectType: 0 ,}, methods: {onSelectType (type) {this. selectType = type }}</script>
The parent component uses @ select-type = "onSelectType" @, which is short for "v-on". The Listener consists of the sub-component vm. $ emit-triggered events use onSelectType () to receive data transmitted from child components, notifying the parent component that the data has changed.
// Sub-component <template> <div> <span @ click = "select (0, $ event)": class = "{'activity ': selectType = 0} "> </span> <span @ click =" select (1, $ event) ": class =" {'active ': selectType = 1} "> </span> <span @ click =" select (2, $ event) ": class =" {'active ': selectType = 2} "> </span> </div> </template> <script> data () {return {selectType: 0,}, methods: {select (type, event) {this. selectType = type this. $ emit ('select-type', type) }}</script>
The child component uses $ emit to trigger the event and pass the parameter.
In vue2.0 # $ emit and $ on described in the above section, I hope to help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!