Example Analysis of vue State sharing
This article mainly introduces the example analysis of vue state sharing, which is very detailed and has certain reference value. Friends who are interested must finish it!
State sharing
With the refinement of components, we will encounter the situation of multi-component state sharing. Vuex can certainly solve these problems, but as stated in the official Vuex documentation, if the application is not large enough, it is best not to use it in order to avoid tedious and redundant code. Today we are introducing the newly added Observable API of vue.js 2.6. by using this api, we can deal with some simple cross-component data state sharing situations.
In the following example, we will create a store outside the component, and then use the store and mutation methods provided by store.js in the App.vue component, as well as other components, so that multiple components can share data state.
First create a store.js that contains a store and a mutations to point to data and processing methods, respectively.
Import Vue from "vue"; export const store = Vue.observable ({count: 0}); export const mutations = {setCount (count) {store.count = count;}}
Then introduce the store.js into the App.vue and use the introduced data and methods in the component
Count: {{count}}
+ 1-1import {store, mutations} from ". / store"; export default {name: "App", computed: {count () {return store.count;}}, methods: {setCount: mutations.setCount}}; these are all the contents of the article "sample Analysis of vue State sharing". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!