【vue2 vuex】store state mutations actions状态管理(草稿一留存)

@/store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
import {
	mdUserMsglist
 } from "@/api/index/index.js"
Vue.use(Vuex); //vue的插件机制
const store = new Vuex.Store({
	// state 表示 需要共享的状态数据
	state: {
		msg: uni.getStorageSync('ismsg'), // 是否有新消息 true是 false否
	},
	/*
		1. mutations 表示 更改 state的方法集合 只能是同步更新 不能写ajax等异步请求
		2. 异步操作,通过mutations来改变state。
		3.包含多个事件回调函数的对象。
		4. 执行方式:通过执行 commit()来触发 mutation 的调用, 间接更新 state
		5.触发方式: 组件中: $store.dispatch(‘action 名称’, data1)
	*/ 
	mutations: {
		// 更新数据
		uploadMsg(state, msgdata) {
			state.msg = msgdata;
		},
	},
	// actions 将对应的网络请求数据放在这里来操作 可以在actions中发起 然后提交给 mutations mutation再做同步更新
	actions: {
		// 请求数据
		getMsg(){
			console.log('请求数据11')
			if(!uni.getStorageSync('id')){ return }
			let data = {
				userId: uni.getStorageSync('id')
			}
			let isnew = false;
			mdUserMsglist(data).then(res => {
				console.log('actions 请求数据 ',res.rows)
				res.rows.forEach(item => {
					if(item.status == 0){
						isnew = true;
					}
				});
				context.commit("uploadMsg",isnew) //拿到数据传入进去
			});
		}
		
	}
})
export default store

main.js

import store from 'store/index.js'

import Vue from 'vue'
App.mpType = 'app'
const app = new Vue({
    ...App,
	store
})
app.$mount()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值