安装:npm install vuex@next --save
main.js引入store
import { createApp } from 'vue'
import App from './App.vue'
import store from './store/store.js'
const app = createApp(App);
app.use(store);
app.mount('#app')
store.js
import { createStore } from 'vuex'
import storage from './storage'
export default createStore({
modules: {
storage: storage
}
})
storage.js
export default {
state: {
id:""
},
mutations: {
setInfo(state, data) {
Object.assign(state, data);
}
},
页面应用
<script>
import { useStore } from "vuex";
export default {
setup() {
const store = useStore();
let res = {id: 888};
store.commit("setInfo", res); //存储
const getStoreId = store.state.storage.id //调用
}
}
</script>