From edae8db814d697964d629e4873f947da87906096 Mon Sep 17 00:00:00 2001 From: "idiotalex@163.com" Date: Mon, 2 Nov 2020 18:09:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20vuex=20=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web-vue/package.json | 3 ++- web-vue/src/main.js | 2 ++ web-vue/src/pages/layout/index.vue | 7 +++++++ web-vue/src/pages/login/index.vue | 3 ++- web-vue/src/store/index.js | 14 ++++++++++++++ web-vue/src/store/modules/user.js | 25 +++++++++++++++++++++++++ 6 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 web-vue/src/store/index.js create mode 100644 web-vue/src/store/modules/user.js diff --git a/web-vue/package.json b/web-vue/package.json index aa89ee61d..e0f82885a 100644 --- a/web-vue/package.json +++ b/web-vue/package.json @@ -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", diff --git a/web-vue/src/main.js b/web-vue/src/main.js index e556e29a9..7c51a1d6f 100644 --- a/web-vue/src/main.js +++ b/web-vue/src/main.js @@ -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') diff --git a/web-vue/src/pages/layout/index.vue b/web-vue/src/pages/layout/index.vue index 0aebc02a5..c72098a6b 100644 --- a/web-vue/src/pages/layout/index.vue +++ b/web-vue/src/pages/layout/index.vue @@ -44,16 +44,23 @@ :style="{ margin: '24px 16px', padding: '24px', background: '#fff', minHeight: '280px' }" > Content + {{ getToken }} diff --git a/web-vue/src/pages/login/index.vue b/web-vue/src/pages/login/index.vue index a75edccdf..8d6c1ede8 100644 --- a/web-vue/src/pages/login/index.vue +++ b/web-vue/src/pages/login/index.vue @@ -63,8 +63,9 @@ export default { message: res.msg, duration: 2 }); + this.$store.dispatch('login'); // 跳转主页面 - this.$router.push({ path: '/' }) + this.$router.push({ path: '/' }); } }) } diff --git a/web-vue/src/store/index.js b/web-vue/src/store/index.js new file mode 100644 index 000000000..d51a2482e --- /dev/null +++ b/web-vue/src/store/index.js @@ -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 diff --git a/web-vue/src/store/modules/user.js b/web-vue/src/store/modules/user.js new file mode 100644 index 000000000..407d47e85 --- /dev/null +++ b/web-vue/src/store/modules/user.js @@ -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