1. Component Communication1. Props parent component ---> child component communication
<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 <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
<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
<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 communication Detailed explanation
/* Parent component */ export default{ provide: return { provideName: 'Selling front-end' } } } At this point, the variable /*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)
<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 management
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { }, mutations: }, actions: { }, modules: } }) This concludes this article about eight ways to use You may also be interested in:
|
<<: How to use mysqldump for full and point-in-time backups
>>: Distinguishing between Linux hard links and soft links
In this article, I will explain the relevant cont...
Commonly used commands for Linux partitions: fdis...
When there are tens of thousands of records in th...
MySQL5.6 How to create SSL files Official documen...
background A specific device is used to perform i...
There is a business that queries the 5 most recen...
Table of contents What is the Linux system that w...
Table of contents What is a Promise? Usage of rej...
XML/HTML CodeCopy content to clipboard < butto...
1. Introduction The ls command is used to display...
<table id=" <%=var1%>">, the...
After purchasing an Alibaba Cloud server, you nee...
Before the release of Microsoft IE 5.0, the bigges...
This article shares the specific code of JavaScri...
Table of contents Angular accomplish Calling orde...