As components become more detailed, you will encounter situations where multiple components share state. Vuex can certainly solve this type of problem, but as the official Vuex documentation says, if the application is not large enough, it is best not to use it to avoid cumbersome and redundant code. Today we will introduce the newly added Observable API in vue.js 2.6. By using this api, we can deal with some simple cross-component data state sharing situations. Creating a store objectFirst, create a store.js, which contains a store and a mutation, which are used to point to data and processing methods respectively. //store.js import Vue from 'vue'; export let store =Vue.observable({count:0,name:'李四'}); export let mutations={ setCount(count){ store.count=count; }, changeName(name){ store.name=name; } } Apply store objects to different componentsThen use the object in your component //obserVable.vue <template> <div> <h1>Cross-component data state sharing observable</h1> <div> <top></top> <bottom></bottom> </div> </div> </template> <script> import top from './components/top.vue'; import bottom from './components/bottom.vue'; export default { name: 'obserVable', components: top, bottom } }; </script> <style scoped> </style> //Component a <template> <div class="bk"> <span ><h1>a component</h1> {{ count }}--{{ name }} > <button @click="setCount(count + 1)">+1 in the current a component</button> <button @click="setCount(count - 1)">Current a component -1</button> </div> </template> <script> import { store, mutations } from '@/store'; export default { computed: { count() { return store.count; }, name() { return store.name; } }, methods: { setCount: mutations.setCount, changeName: mutations.changeName } }; </script> <style scoped> .bk { background: lightpink; } </style> //Component b <template> <div class="bk"> <h1>b component</h1> {{ count }}--{{ name }} <button @click="setCount(count + 1)">+1 in current b component</button> <button @click="setCount(count - 1)">Current b component -1</button> </div> </template> <script> import { store, mutations } from '@/store'; export default { computed: { count() { return store.count; }, name() { return store.name; } }, methods: { setCount: mutations.setCount, changeName: mutations.changeName } }; </script> <style scoped> .bk { background: lightgreen; } </style> Display Effect This concludes this article on the use of Vue.observable() for Vue local component data sharing. For more relevant Vue.observable() data sharing 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:
|
>>: CentOS 6 Compile and install ZLMediaKit analysis
Table of contents 1. Shared and Exclusive Locks 2...
1. Float: The main purpose is to achieve the effe...
Download Download address: https://dev.mysql.com/...
Passive Check With passive health checks, NGINX a...
1. Division of labor and process <br />At T...
1. An error (1064) is reported when using mysqldu...
Table of contents Initialization of echart app-ba...
Today a junior student asked a question. The HTML...
Table of contents Implementation ideas: Step 1: C...
For Centos installation of MySQL, please refer to...
To search for RocketMQ images, you can search on ...
Cascading and Cascading Levels HTML elements are ...
This article example shares the specific code of ...
Table of contents 1. Overview 2. gdb debugging 2....
1. The color of the scroll bar under xhtml In the ...