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
Python connects to MySQL to modify and query data...
Preface Sometimes you need to keep the height of ...
If you want the path following the domain name to...
Table of contents Deploy tomcat 1. Download and d...
This article shares the specific code for WeChat ...
A brief description of environment variable confi...
Table of contents Introduction Traditional transi...
1. W3C versions of flex 2009 version Flag: displa...
This article example shares the specific code of ...
Source: https://medium.com/better-programming, au...
When the same function and HTML code are used mul...
I've been using Bootstrap to develop a websit...
1. Remove backslashes through the "stripslas...
The decompressed version of mysql5.7.18 starts th...
Table of contents 1. View hook 1. Things to note ...