添加 vuex 状态管理

This commit is contained in:
idiotalex@163.com 2020-11-02 18:09:30 +08:00
parent a6f3a90532
commit edae8db814
6 changed files with 52 additions and 2 deletions

View File

@ -14,7 +14,8 @@
"qs": "^6.9.4",
"sha1": "^1.1.1",
"vue": "^2.6.11",
"vue-router": "^3.4.8"
"vue-router": "^3.4.8",
"vuex": "^3.5.1"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.5.0",

View File

@ -6,11 +6,13 @@ import 'ant-design-vue/dist/antd.css';
import './assets/reset.css';
import router from './router';
import store from './store';
Vue.config.productionTip = false;
Vue.use(Antd);
new Vue({
router,
store,
render: h => h(App),
}).$mount('#app')

View File

@ -44,16 +44,23 @@
:style="{ margin: '24px 16px', padding: '24px', background: '#fff', minHeight: '280px' }"
>
Content
{{ getToken }}
</a-layout-content>
</a-layout>
</a-layout>
</template>
<script>
import { mapGetters } from 'vuex';
export default {
data() {
return {
collapsed: false
}
},
computed: {
...mapGetters([
'getToken'
])
}
}
</script>

View File

@ -63,8 +63,9 @@ export default {
message: res.msg,
duration: 2
});
this.$store.dispatch('login');
//
this.$router.push({ path: '/' })
this.$router.push({ path: '/' });
}
})
}

View File

@ -0,0 +1,14 @@
import Vuex from 'vuex';
import Vue from 'vue';
import user from './modules/user';
Vue.use(Vuex);
const store = new Vuex.Store({
modules: {
user
}
})
export default store

View File

@ -0,0 +1,25 @@
const TOKEN_KEY = 'JpomToken';
const user = {
state: {
token: localStorage.getItem(TOKEN_KEY)
},
mutations: {
setToken(state, token) {
state.token = token
}
},
actions: {
login({commit}, token) {
commit('setToken', token);
localStorage.setItem(TOKEN_KEY, token);
}
},
getters: {
getToken(state) {
return state.token;
}
}
}
export default user