话不多说上代码:
store.jsimport {createStore} from "vuex";
import tokenModule from "@/store/modules/token";
const store = createStore({
modules: {
tokenModule
}
});
export default store;
token.jsconst tokenModule = {
namespaced: true,
state: {
userToken: "success"
},
mutations: {
},
getters: {
getUserTokenStr(state) {
return state.userToken;
}
},
actions: {
}
};
export default tokenModule;
使用mapGetters方法获取<script>
import { mapGetters } from 'vuex';
export default {
data() {
return {
}
},
mounted() {
this.getUserToken()
},
methods: {
...mapGetters('tokenModule',[
'getUserTokenStr'
]),
getUserToken() {
let token = this.getUserTokenStr();
console.log(token);
}
}
}
</script>
|