1. From father to son Define the Parent Component <template> <div> <HelloWorld :title="msg" /> </div> </template> <script> import HelloWorld from "../components/HelloWorld.vue"; export default { name: "Home", data() { return { msg: "Search Music", }; }, components: HelloWorld, }, }; </script> Subcomponents <template> <div class="hello"> <h1>{{ title }}</h1> </div> </template> <script> export default { name: "HelloWorld", props:["title"], data() { return {}; }, }; </script> 2. Son to Father To pass a value from child to parent, you need to trigger an event in the child component. In the event, call Subcomponents <template> <div class="hello"> <h1 @click="add">{{ title }}</h1> </div> </template> <script> export default { name: "HelloWorld", props: ["title"], data() { return { age:18 }; }, methods: { add(){ this.$emit("childEvent", this.age); } }, }; </script> Parent Component <template> <div> <HelloWorld @childEvent="parentEvent" :title="msg" /> </div> </template> <script> import HelloWorld from "../components/HelloWorld.vue"; export default { name: "Home", data() { return { msg: "Search Music", }; }, methods: { parentEvent(e) { console.log(e); }, }, components: HelloWorld, }, }; </script> 3. Brother component value transfer 1. First create a import Vue from 'vue'; export default new Vue; 2. Introduce <template> <div class="hello"> <h1 @click="add">{{ title }}</h1> </div> </template> <script> import bus from "../publicFn/bus.js"; export default { name: "HelloWorld", props: ["title"], data() { return { age:18 }; }, methods: { add(){ bus.$emit("childEvent", this.age); } }, }; </script> 3. Use <template> <div id='swiper'> <button>I am a button</button> </div> </template> <script> import bus from "../publicFn/bus.js"; export default { name:'Swiper', data (){ return { } }, mounted(){ bus.$on("childEvent", (e) => { console.log(e) }) } } </script> 4. Parent components use data and methods of child components 1. Write the 2. The parent component can access the child component through Parent Component <template> <div> <HelloWorld :title="msg" ref="hello" /> <button @click="parentEvent">I am a father</button> </div> </template> <script> import HelloWorld from "../components/HelloWorld.vue"; export default { name: "Home", data() { return { msg: "Search Music", }; }, methods: { parentEvent() { this.$refs.hello.add(); console.log(this.$refs.hello.age); }, }, components: HelloWorld }, }; </script> Subcomponents <template> <div class="hello"> <h1>{{ title }}</h1> </div> </template> <script> export default { name: "HelloWorld", props: ["title"], data() { return { age:18 }; }, methods: { add(){ console.log("I am a child component"); } }, }; </script> 5. Child components use the data and methods of parent components In a child component, you can use Parent Component <template> <div> <HelloWorld :title="msg" ref="hello" /> </div> </template> <script> import HelloWorld from "../components/HelloWorld.vue"; export default { name: "Home", data() { return { msg: "Search Music", }; }, methods: { parentEvent() { console.log("I am the method of the parent component"); }, }, components: HelloWorld }, }; </script> Subcomponents <template> <div class="hello"> <h1 @click="add">{{ title }}</h1> </div> </template> <script> export default { name: "HelloWorld", props: ["title"], data() { return { age:18 }; }, methods: { add(){ console.log(this.$parent.msg) this.$parent.parentEvent(); } }, }; </script> 6. Vuex value transfer 6.1, define the store import Vue from "vue"; import Vuex from "vuex"; Vue.use(Vuex); export default new Vuex.Store({ state: { school: "Tsinghua University", a:"nice" }, getters: { returnVal(state) { return state.school + state.a; }, }, mutations: changeSchool(state, val) { state.school = val; console.log('Modification successful'); }, }, actions: {}, modules: {} }); 6.2, Mount import Vue from 'vue'; import App from './App.vue'; import router from "./router"; import store from "./store"; import ElementUI from "element-ui"; import "element-ui/lib/theme-chalk/index.css"; import publicFn from "./publicFn/publicFn"; Vue.config.productionTip = false const url = process.env.VUE_APP_URL; Vue.prototype.$url = url; Vue.prototype.$publicFn = publicFn; Vue.use(ElementUI); new Vue({ router, store, render: h => h(App), }).$mount('#app') 6.3, Use <template> <div class="hello"> <h1 @click="add">{{ title }}</h1> </div> </template> <script> export default { name: "HelloWorld", props: ["title"], data() { return { age:18 }; }, methods: { add(){ console.log(this.$store.state.school);//Get the value//this.$store.commit('changeSchool', 'Peking University');//Modify the value// console.log(this.$store.getters.returnVal)//Get the filtered value} }, }; </script> 7. Routing value7.1 Passing values through query Note: This method will not lose the page refresh parameters, and will display the parameters after the address bar, Page A <template> <div> <HelloWorld :title="msg" ref="hello" /> <button @click="parentEvent">Jump</button> </div> </template> <script> import HelloWorld from "../components/HelloWorld.vue"; export default { name: "Home", data() { return { msg: "Search Music", }; }, methods: { parentEvent() { this.$router.push({ path:"/conter", name:'conter', query:{ id:10086, name:"Peng Duoduo" } }) }, }, components: HelloWorld }, }; </script> Page B <template> <div id='conter'> </div> </template> <script> export default { name:'conter', data (){ return { } }, created (){ console.log(this.$route.query.id, this.$route.query.name); }, } </script> 7.2 Passing values via params Note: When refreshing the page in this way, the parameters will be lost, but they can be received and stored in A-page <template> <div> <HelloWorld :title="msg" ref="hello" /> <button @click="parentEvent">Jump</button> </div> </template> <script> import HelloWorld from "../components/HelloWorld.vue"; export default { name: "Home", data() { return { msg: "Search Music", }; }, methods: { parentEvent() { this.$router.push({ path:"/conter", name:"conter", params:{ id:10086, name:"Peng Duoduo" } }) }, }, components: HelloWorld }, }; </script> Page B <template> <div id='conter'> </div> </template> <script> export default { name:'conter', data (){ return { } }, created (){ console.log(this.$route.params.id, this.$route.params.name); }, } </script> This concludes this article about Vue's seven value-transferring methods. For more information about Vue's value-transferring methods, please search 123WORDPRESS.COM's previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Install OpenSSH on Windows and log in to the Linux server by generating an SSH key
>>: Detailed explanation of the basic usage of SSH's ssh-keygen command
My environment: 3 centos7.5 1804 master 192.168.1...
1. <body> tag: Used to mark the main body o...
This article shares the specific code of js to re...
Table of contents introduction Cookie What are Co...
Preface Every time you use Docker to start a Hado...
In addition to B-Tree indexes, MySQL also provide...
illustrate: Root and alias in location The root d...
Let me summarize a problem that I have encountere...
1. Find mysqldump.exe in the MySQL installation p...
There is often a scenario where the image needs t...
Grayscale release refers to a release method that...
Table of contents Overview 1. Define store.js 2. ...
This article records the detailed process of down...
Table of contents Preface: accomplish: Summarize:...
<br /> English original: http://desktoppub.a...