Best practices are using State in Vuex for unified data storage and secure and standardized data modification and retrieval. You can read more here: https://v3.vuex.vuejs.org/#what-is-a-state-management-pattern.
If you using State in Vuex and Vue 3 you can:
store.js
import 'es6-promise/auto';
import {createStore} from 'vuex'
export const store = createStore({
state () {
return {
count: 1,
}
},
mutations: {
incrementCount (state) {
state.count++
},
setCount (state, newValue) {
state.count = newValue;
},
},
getters: {
getCount: state => {
return '*' + state.count + '*';
}
}
})
app.js
import {store} from "./store";
const app = createApp({...})
app.use(store);
export const vue_app = app.mount('#vue_app');
and in Javascript to modify:
const store = vue_app.__vue_app__._context.provides.store; // Store instance
store._state.data.count; // get value
store.getters.getCount; // get value by the getter
store._mutations.incrementCount[0](); // modification by the mutation "incrementCount"
store._mutations.setCount[0](123); // modification by the mutation "setCount"
store._state.data.count = 123; // direct modification