For Vue, message passing between components is very important. Here is my summary of the common ways of message passing between components: 1. Props parent component ---> child component communicationParent component---property transfer to child component Subcomponents---receive data in props mode <Son :datas="fData"></Son> <script> import Son from '@/components/son' export default{ name:'Father', components:{Son}, data(){ return { fData:'I am the value passed from the parent component to the child component - props method' } } } </script> The parameter name accepted by the child component props must be consistent with the property name defined when the parent component is passed <template> <div>I am the data of the parent component: {{fData}}</div> <div @click=changeData>I am the parent component passing the modified data: {{mydata}}</div> </template> <script> export default{ name:'Son', props:{ fData:{ type:String, default:'' } } data(){ mydata:this.fatherData }, methods:{ changeData(){ this.mydata += 'Change data' } }, } </script> Notice:
props:{ fData:{ type:[String,Number], default:'' } } 2. $emit child component ---> parent component transfer
Subcomponents <el-button @click="handleEmit">Change parent component</el-button> <script> export default{ name:'Son', methods:{ handleEmit(){ this.$emit('triggerEmit','subcomponent data') } } } </script> Parent component (the event name sent by the child component must be consistent with the event name accepted by the parent component) <Son @triggerEmit="changeData"></Son> <script> import Son from '@/components/son' export default{ name:'Father', components:{Son}, methods:{ changeData(name){ console.log(name) // => I am the data from the child component} } } </script> $emit and props combined with sibling components to pass values
Parent Component <childA :myName="name"></ChildA> <ChildB :myName="name" @changeName="editName"></ChildB> export default{ data() { return { name: 'Hello Data' } }, methods: { editName(name){ this.name = name } } } Subcomponent B changes and receives data <p>Name: {{ myName }}</p> <button @click="changeName">Change name</button> <script> export default{ props: { myName:String }, methods: { changeName() { this.$emit('changeName', 'new data name') } } } </script> Subcomponent A receives data <p>Name: {{ newName }}</p> <script> export default{ props: { myName:String } } </script> 3. Bus (Event Bus) Brother Component CommunicationTransferring values between components other than parent-child components or between components at more levels. In Vue, separate event centers are used to manage the transfer of values between components.
import Vue from "vue" const bus = new Vue() export default bus Define the call in the component that needs to change the data <template> <div> <div>I am communication component A</div> <button @click="changeName">Change name</button> </div> </template> <script> import bus from "@/utils/Bus.js"; export default { components: {}, data() { return {}; }, mounted() { console.log(bus); }, methods: { changeName() { bus.$emit("editName", "dataset!"); }, }, }; </script> <style lang='scss' scoped> </style> The bus.js file is also introduced in another component, and the event callback is monitored through $on <template> <div> <span>Name: {{name}}</span> <div>I am communication component B</div> </div> </template> <script> import bus from "@/utils/Bus.js"; export default { components: {}, data() { return {name}; }, mounted() { bus.$on("editName", (name) => { this.name=name console.log(name); // }); }, methods: {}, }; </script> <style lang='scss' scoped> </style> 4. $parent, $children directly access component instances
Child component --- this.$parent can obtain the parent component's methods, data, etc., and can be used and executed directly <template> <div>I am a child component</div> </template> <script> export default{ name:"Son", data(){ return { sonTitle: 'I am the data of the child component' } }, methods:{ sonHandle(){ console.log('I am a method of a subcomponent') } }, created(){ console.log(this.$parent) console.log(this.$parent.fatherTitle) // => I am the data of the parent component this.$parent.fantherHandle() // => I am the method of the parent component } } </script> Parent component --- Gets the child component instance, and the instance obtained is in the form of an array. This.$children[0] can get a component instance and call component methods and data <template> <div> <Son>I am the parent component</Son> </div> </template> <script> import Son from './son.vue' export default{ name: 'father', components:{ Son }, data(){ return { fatherTitle: 'I am the data of the parent component' } }, methods:{ fantherHandle(){ console.log('I am the method of the parent component') } }, mounted(){ console.log(this.$children) console.log(this.$children[0].sonTitle) // => I am the data of the child component this.$children[0].sonHandle() // => I am the method of the child component } } </script> 5. $refsref is used to register reference information for an element or subcomponent. The reference information will be registered on the $refs object of the parent component. The parent component uses $refs to get the component instance <template> <div> <Son ref="son"></Son> </div> </template> <script> import Son from './son.vue' export default{ name: 'father', components:{ Son }, mounted(){ console.log(this.$refs.son) /*Component instance*/ } } </script> 6. provide/inject (provide/inject) multi-component or deep component communicationDetailed explanation of provide/inject
/* Parent component */ export default{ provide: return { provideName: 'Selling front-end' } } } At this point, the variable provideName can be provided to all its child components, including great-grandchildren, grandchildren, etc., and data can be obtained by simply using inject /*Subcomponent*/ export default{ inject: ['provideName'], created () { console.log(this.provideName) // => "Selling front-end" } }
7. slot (slot-scope scope slot) child element --> parent element (similar to communication)
Child Elements <template> <div> <div class="isSon"> <slot :info='arrList'></slot> </div> </div> </template> <script> export default { components: {}, data() { return {arrList:[1,'aa','张三']}; }, mounted() { }, methods: { }, }; </script> Parent element <template> <div> <SonG> <span slot-scope="props"> <ul> aa <li v-for="item in props.info" :key="item"> {{item}} </li> </ul> </span> </SonG> </div> </template> <script> import SonG from '../components/SonG.vue' export default { components:{ SonG }, data () { return { } } } </script> 8. Vuex state managementEquivalent to a public data warehouse Provide some methods to manage warehouse data import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { }, mutations: }, actions: { }, modules: } }) SummarizeThis concludes this article about eight ways to implement component communication in Vue. For more relevant content about implementing component communication in Vue, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to set mysql to case insensitive
>>: 4 ways to view processes in LINUX (summary)
What is the input type="file"? I don'...
<br />When the page contains <img src=&qu...
This article shares the specific code of JavaScri...
The most important interactive design article in ...
The simple installation configuration of mysql5.7...
Lots of links You’ve no doubt seen a lot of sites ...
Preface In the early stages of some projects, dev...
Without further ado, I will post the code for you...
Table of contents this Method In the object Hidde...
A web server can build multiple web sites with in...
When creating a time field DEFAULT CURRENT_TIMEST...
What is margin-top collapse Margin-top collapse i...
When there are tens of thousands of records in th...
Mysql-connector-java driver version problem Since...
Quickstart Guide The Foreman installer is a colle...