vuex的几种常用方法

1. vuex安装

安装vuex(uniapp跳过)

npm install vuex

2. 无getters实现

主仓

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import user from './modules/index.js'
const store = new Vuex.Store({
	modules: {
		user,
	}
})

export default store

子仓

下面是一个uniapp的免登陆的一部分

import store from '../index.js'

const state = {
	user: {}
}
const actions = {
	loginByUser({commit},provide){
		uni.getStorage({
			key:"user",
			success:(res => {
				commit("LOGIN_BY_USER",res.data)
			})
		})
	}
}
const mutations = {
	CHANGE_USER(state, provide) {
		state.user = provide
		uni.setStorage({
			key:"user",
			data:state.FrdMsglist
		})
	},
	LOGIN_BY_USER(state, provide) {
		state.user = provide
	},
}

export default {
	mutations,
	state,
	actions
}

vue中没有uni.getStorage进行持久化,这里可以使用good-storage

npm install good-storage
//在这里可以进行预置一些东西
import storage from 'good-storage'
function insert(key, val) {
  let ss = storage.get(key)
  ss.status = val;
  storage.set(key,ss)
  if (typeof storage.get(key) != 'undefined') {
    return true
  }
  return false
}
function select(key) {
  return {'val': storage.get(key), 'key': key};
}

function remove(key) {
  if (typeof storage.get(key) == 'undefined') {
    return "null"
  }
  storage.remove(key);
  if (typeof storage.get(key) == 'undefined') {
    return true
  }
  return false
}
export {
  insert,
  select,
  remove,
}

使用

方法一: 使用mapActions、mapMutations就可以直接调用
<script>
	import {mapMutations,mapActions,mapState} from 'vuex'
	export default {
		onLaunch: function() {
			this.loginByUser()
			//let user = {id:'123',name:'哈哈'}
			//this.CHANGE_USER(user)
		},
		onShow: function() {
		},
		onHide: function() {
			
		},
		computed:{
			...mapState({
     			user: (state) => state.user.user,//是不是感觉像getters,第一个user是子库,第二个user是值
    		})
		},
		methods:{
		...mapMutations(['loginByUser']),
		...mapActions(['CHANGE_USER'])
		}
	}
</script>
方法二
<script>
	export default {
		onLaunch: function() {
			this.loginByUser()
			//let user = {id:'123',name:'哈哈'}
			//this.CHANGE_USER(user)
		},
		onShow: function() {
		},
		onHide: function() {
			
		},
		computed:{
		},
		methods:{
			login(){
				let user = {id:'123',name:'哈哈'}
				this.$store.commit("CHANGE_USER",user) //调用mutations中的函数
				this.$store.dispatch('loginByUser')//调用actions中的函数
				this.user = this.$store.state.user.user //调出state中的值
			}
		}
	}
</script>

getters

getters文件

const getters = {
  user: state => state.modules.user,
}
export default getters
主仓
import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
Vue.use(Vuex)

const modulesFiles = require.context('./modules', true, /\.js$/)
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(modulePath)
  modules[moduleName] = value.default
  return modules
}, {})

const store = new Vuex.Store({
  modules,
  getters
})

export default store

子仓与上面一样
使用
<script>
	export default {
		onLaunch: function() {
			this.loginByUser()
			//let user = {id:'123',name:'哈哈'}
			//this.CHANGE_USER(user)
		},
		onShow: function() {
		},
		onHide: function() {
			
		},
		computed:{
		},
		methods:{
			login(){
				this.user = this.$store.getters.user //调出state中的值
			}
		}
	}
</script>

getters的封装只是对于state中的值进行从新赋值,在调用actions和mutations中没有明显变化
uni.getStorage与good-storage进行数据持久化时,要注意,添加的时候如果已经存在,则直接替换value值

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vace cc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值