当前位置:   article > 正文

Vuex存储公共的数据步骤_前端vue存储公共数据

前端vue存储公共数据

在我们平常做项目的过程中,经常会使用到公共的数据,这个时候Vuex是一个很好的仓库,它可以用来存储公共的数据 。

总共有五个: state ,getters,muaction,action,module。

现在我们具体分析一下怎么去使用。

一.在Store下的index.js

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import getters from './getters'
  4. import app from './modules/app'
  5. import settings from './modules/settings'
  6. import user from './modules/user'
  7. Vue.use(Vuex)
  8. const store = new Vuex.Store({
  9. modules: {
  10. app,
  11. settings,
  12. user
  13. },
  14. getters
  15. })
  16. export default store

分析:

  1. import getters from './getters'
  2. import app from './modules/app'
  3. import settings from './modules/settings'
  4. import user from './modules/user'

导入并且注册成模块。

二.在注册的user模块下

  1. // 将token存储vuex中,vuex刷新就没了,解决刷新就没了的问题
  2. import Cookiejs from 'js-cookie'
  3. import { sysLogin } from '@/api/login'
  4. import { Message } from 'element-ui'
  5. import { sysProfile } from '@/api/user'
  6. const state = {
  7. // 有就取token 没有就为空
  8. token: Cookiejs.set('hr67') || '',
  9. userInfo: ''
  10. }
  11. const mutations = {
  12. setToken(state, value) {
  13. state.token = value
  14. Cookiejs.set('hr67', value)
  15. },
  16. setUserInfo(state, value) {
  17. state.userInfo = value
  18. }
  19. }
  20. const actions = {
  21. // 登录
  22. async toLogin(store, from) {
  23. const res = await sysLogin(from)
  24. Message.success('登录成功')
  25. store.commit('setToken', res.data)
  26. },
  27. // 获取用户信息
  28. async getUserInfo({ commit }) {
  29. const res = await sysProfile()
  30. commit(' setUserInfo', res.data)
  31. }
  32. }
  33. const getters = {}
  34. export default {
  35. namespaced: true,
  36. state,
  37. mutations,
  38. getters,
  39. actions
  40. }

分析:

1.在state里面定义变量

  1. const state = {
  2. // 有就取token 没有就为空
  3. token: Cookiejs.set('hr67') || '',
  4. userInfo: ''
  5. }

2.在mutations下定义修改变量的方法

  1. const mutations = {
  2. setToken(state, value) {
  3. state.token = value
  4. Cookiejs.set('hr67', value)
  5. },
  6. setUserInfo(state, value) {
  7. state.userInfo = value
  8. }
  9. }

3.在action调用接口获取数据

  1. const actions = {
  2. // 登录
  3. async toLogin(store, from) {
  4. const res = await sysLogin(from)
  5. Message.success('登录成功')
  6. store.commit('setToken', res.data)
  7. },
  8. // 获取用户信息
  9. async getUserInfo({ commit }) {
  10. const res = await sysProfile()
  11. commit(' setUserInfo', res.data)
  12. }
  13. }

分析

sysLogin方法sysProfile这两个方法都通过:

  1. import { sysLogin } from '@/api/login'
  2. import { sysProfile } from '@/api/user'

导入进来了

通过commit来触发mutations里面的方法。

然后在mutation里面进行变量的修改。

触发state的唯一方法,就是mutations。

这是在本模块进行触发的。

如果在其它的页面进行触发呢?

假设点击登录页面进行触发:

  1. submit() {
  2. this.$refs.form.validate(async(result) => {
  3. if (result) {
  4. // const res = await sysLogin(this.form)
  5. // this.$store.commit('user/setToken', res.data)
  6. // console.log(res)
  7. // this.$message.success('验证成功')
  8. await this.$store.dispatch('user/toLogin', this.form)
  9. this.$router.push('/')
  10. }
  11. })
  12. }

格式:

通过  this.$Store.dispatch('模块名/方法名',参数)。

然后,通过toLogin里面的commit方法来去触发actions进行修改

总结:

1.修改state唯一的方式就是通过actions

2.触发mutations的方法是通过commit方法

3.触发actions的方法是通过dispatch。

4.如果注册成了模块在其它页面进行触发,需要加上模块的名字

 举例:

await this.$store.dispatch('user/toLogin', this.form)

在登录的页面进行触发。

5. 通常情况下先触发actions来调用接口获取数据,然后在再actions里面通过commit去

触发mutations方法从而达到修改state数据的目的。

4.getters

这个类似于计算属性

5.modules

注册成不同的模块        

6.view图

 

因此,我们为什么不把组件的共享状态抽取出来,以一个全局单例模式管理呢?在这种模式下,我们的组件树构成了一个巨大的“视图”,不管在树的哪个位置,任何组件都能获取状态或者触发行为!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/701062
推荐阅读
相关标签
  

闽ICP备14008679号