Problem to be solvedMainly for cross-level communication between components Why do you need to implement dispatch and broadcast yourself?Because when developing independent components or libraries, it is best not to rely on third-party libraries Why not use provide and inject? Because its usage scenario is mainly for sub-components to obtain the status of parent components, an active provision and dependency injection relationship is established between cross-level components. The code is as follows: emitter.js function broadcast(componentName, eventName, params) { this.$children.forEach(child => { const name = child.$options.name; if (name === componentName) { child.$emit.apply(child, [eventName].concat(params)); } else { // todo If params is an empty array, it will receive undefined broadcast.apply(child, [componentName, eventName].concat([params])); } }); } export default { methods: { dispatch(componentName, eventName, params) { let parent = this.$parent || this.$root; let name = parent.$options.name; while (parent && (!name || name !== componentName)) { parent = parent.$parent; if (parent) { name = parent.$options.name; } } if (parent) { parent.$emit.apply(parent, [eventName].concat(params)); } }, broadcast(componentName, eventName, params) { broadcast.call(this, componentName, eventName, params); } } }; parent.vue <template> <div> <h1>I am the parent component</h1> <button @click="handleClick">Trigger event</button> <child /> </div> </template> <script> import Emitter from "@/mixins/emitter.js"; import Child from "./child"; export default { name: "componentA", mixins: [Emitter], created() { this.$on("child-to-p", this.handleChild); }, methods: { handleClick() { this.broadcast("componentB", "on-message", "Hello Vue.js"); }, handleChild(val) { alert(val); } }, components: Child } }; </script> child.vue <template> <div>I am a child component</div> </template> <script> import Emitter from "@/mixins/emitter.js"; export default { name: "componentB", mixins: [Emitter], created() { this.$on("on-message", this.showMessage); this.dispatch("componentA", "child-to-p", "hello parent"); }, methods: { showMessage(text) { window.alert(text); } } }; </script> In this way, cross-level component custom communication can be achieved. However, one problem should be noted: subscription must precede publishing, that is, on must come before emit. Parent-child component rendering order, instance creation order The child component is rendered before the parent component, so when the mounted event of the child component is dispatched, it cannot be heard in the mount in the parent component. This is the end of this article about Vue's self-implemented dispatch and broadcast (dispatch and broadcast). For more relevant Vue dispatch and broadcast content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to change the root password in MySQL 5.7
>>: How to implement remote automatic backup of MongoDB in Linux
Table of contents Preface Error Object throw try…...
Table of contents Component recursive call Using ...
Preface "High Performance MySQL" mentio...
Table of contents 01. Use useState when render is...
Preface During the development process, we often ...
Preface Many years ago, I was a newbie on the ser...
There is a simple CSS method to realize the pop-u...
Although W3C has established some standards for HT...
<tr> <th width="12%">AAAAA&l...
Table of contents Preface Code Implementation Ide...
Preface I am a PHP programmer who started out as ...
Application scenario 1: Domain name-based redirec...
1. Software Download MySQL download and installat...
This tutorial shares the installation and configu...
1. Download address: mysql-8.0.17-winx64 Download...