1.index.js
javascript
import user from './modules/user.js'
import {
createStore
} from 'vuex'
// todo ssr
const store = createStore({
modules: {
user,
}
})
export default store
2.user.js
javascript
export default {
namespaced: true,
state: {
token:"", //token
userId:"", //用户id
userName:"",//用户姓名
},
mutations: {
SET_TOKEN(state,token){
state.token = token
uni.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 = id
uni.setStorageSync("userId",id)
},
SET_USERNAME(state,name){
state.userName = name
uni.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
javascript
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
}
}