1.index.js
import user from './modules/user.js'import {createStore
} from 'vuex'
// todo ssr
const store = createStore({modules: {user,}
})export default store
2.user.js
export default {namespaced: true,state: {token:"", //tokenuserId:"", //用户iduserName:"",//用户姓名},mutations: {SET_TOKEN(state,token){state.token = tokenuni.setStorageSync("token",token)},LOGIN_OUT(state){state.token = ""uni.removeStorageSync("token")//uni.clearStorageSync()uni.reLaunch({url:"/pages/index/index"})},SET_USERID(state,id){state.userId = iduni.setStorageSync("userId",id)},SET_USERNAME(state,name){state.userName = nameuni.setStorageSync("userName",name)},},actions: {setToken({commit},token){commit("SET_TOKEN",token)},setUserId({commit},id){commit("SET_USERID",id)},loginOut({commit}){commit("LOGIN_OUT")},}
}
3.main.js
import store from './store'
import { createSSRApp } from 'vue'
import App from './App.vue'
export function createApp() {const app = createSSRApp(App)app.use(store)return {app}
}