Without using Vuex, the way to pass values between components is through parent-to-child or sibling components. From father to son:fatherComponent: <template> <div> <HELLOWORLD :needData="content"></HELLOWORLD> </div> </template> <script> import HELLOWORLD from '../components/HelloWorld.vue' export default { components:{ HELLOWORLD }, data(){ return { content:"content" } } } </script> <style lang="less" scoped> </style> SonComponent (the subcomponent name is HELLOWORLD): <template> <div> <h1>HELLOWORLD</h1> </div> </template> <script> export default { props:["needData"], data(){ return { H:this.needData, } }, mounted(){ console.log(this.H); } } </script> <style lang="less" scoped> </style> Son to Father:FatherComponent: <template> <div> <HELLOWORLD @sendData="getData"></HELLOWORLD> </div> </template> <script> import HELLOWORLD from '../components/HelloWorld.vue' export default { components:{ HELLOWORLD }, data(){ return { } }, methods:{ getData(sonData){ console.log("data=>",sonData); }, } } </script> <style lang="less" scoped> </style> SonComponent: <template> <div> <h1>HELLOWORLD</h1> </div> </template> <script> export default { data(){ return { content:"content" } }, mounted(){ this.$emit("sendData",this.content); } } </script> <style lang="less" scoped> </style> Effect picture: In fact, in order to transfer data between parent and child components, data can also be transferred by calling the function of the parent component or the function of the child component. In Vue, the child component calls the parent component's function https://www.jb51.net/article/134732.htm Vue parent component calls the function of the child component https://www.jb51.net/article/219793.htm Vuex is an integral part of the Vue framework;
There are five properties in Vue's store.js: The structure is: let a={ state: { name:"moduleA" }, //mutations are specifically used to change the data in the state attribute mutations: { setFun(state,item){ state.name=item; } } } export default new Vuex.Store({ //state is dedicated to storing datastate: { num:100, useAcomponent:{ name:"A", }, useBcomponent:"content", }, //mutations are specifically used to change the data in the state attribute mutations: { setStateFun(state,item){ state.useBcomponent="Bcomponent"; } }, actions: { httpGetData(store,item){ setTimeout(()=>{ console.log(item); store.commit("setStateFun",item); },3000) } }, getters:{ //No parameter is passed when calling the function in getters getterFun1(state){ return state.num++ } //When calling the function in getters, there are input parameters gettterFun2(state){ return function(val){ return state.num+=val; } } }, modules: ModuleA:a } }); } The data in the state can be accessed in different components. Get state data: Data in this.$store.state.state object; For example, let val = this.$store.state.num; To change the state data, just call the function in the mutations object of Vuex: this.$store.commit("function name","data"); For example, this.$store.commit("setStateFun","testSetItem"); actions object, used to make requests in Vuex this.$store.dispatch("function name","data"); For example, this.$store.dispatch("httpGetData","testItem"); Getters object, similar to Vue's calculated properties this.$store.getters.function name; For example // when no parameter is entered this.$store.getters.getterFun1; //When there is an input parameter this.$store.getters.getterFun2(123); The modules object is similar to separating the store modules that need to be used. Each modules object corresponds to a module. //Get the state data in the modules object this.$store.state.modules object name.state value; For example this.$store.state.ModuleA.name //Use the mutations function in the modules object this.$store.commit("function name","input parameter data"); For example, this.$store.commit("setFun","itemabc"); //It should be noted here that if there is a function with the same name in the modules module and in the external (not modules object module) mutations object, both functions with the same name will be executed when called This concludes this article on briefly discussing value transfer between Vue components (including Vuex). For more content on value transfer between Vue components, please search 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:
|
<<: Tutorial on installing MySQL 8.0.11 using RPM on Linux (CentOS7)
>>: Basic usage of UNION and UNION ALL in MySQL
1. Interconnection between Docker containers Dock...
Learn about similar methods for getting character...
In order to provide high availability of the netw...
Table of contents Preface What are dynamic proper...
The domestic market still has a certain demand fo...
This article is welcome to be shared and aggregat...
You can often see articles about CSS drawing, suc...
1. Big Data and Hadoop To study and learn about b...
This article shares the specific code for JavaScr...
Docker is an open source container engine that he...
VMware version: VMware-workstation-full-16 VMware...
Adding indexes can improve query efficiency. Addi...
Table of contents el-scrollbar scroll bar el-uplo...
Beginners can learn HTML by understanding some HT...
Table of contents Preface 1. Key Elements of a Re...