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
If you open some Microsoft documents with LibreOf...
This article uses an example to describe the simp...
Scenario: A laradock development environment (php...
Trigger Introduction A trigger is a special store...
The hyperlink <a> tag represents a link poin...
This article shares the specific code of JavaScri...
1. Install xshell6 2. Create a server connection ...
Today, we use uniapp to integrate Echarts to disp...
Table of contents 1. Learn to return different da...
The effect is as follows: analyze 1. Here you can...
This article example shares the specific code of ...
The implementation of custom carousel chart using...
MySQL reports an error when executing multi-table...
To beautify the table, you can set different bord...
This article shares the MySQL installation and co...