123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- // 页面路径:store/index.js
- import Vue from 'vue'
- import Vuex from 'vuex'
- import persistedstate from 'vuex-persistedstate'
- Vue.use(Vuex); //vue的插件机制
- //Vuex.Store 构造器选项
- const store = new Vuex.Store({
- actions: {
- //相当于异步的操作,不能直接改变state的值,只能通过触发mutations的方法才能改变
- asyncUpdateUser(context, user) {
- context.commit('updateUser', user);
- },
- asyncUpdatemsg(context, data) {
- context.commit('updatemsg', data);
- },
- asyncDelMsg(context, data) {
- context.commit('delMsg', data);
- },
- asyncDelAll(context, data) {
- context.commit('delAll', data);
- }
- },
- mutations: {
- //相当于同步的操作
- updateUser(state, user) {
- // console.log('啊啊啊啊啊');
- state.user = user;
- },
- // 修改头像
- updateAvatar(state, data) {
- state.user.avatar = data;
- },
- // 修改昵称nc
- updateNc(state, data) {
- state.user.nc = data;
- },
- // 添加消息提示
- updatemsg(state, msg) {
- console.log('data:',msg);
- state.msgpop.push(msg);
- // state.msgpop.splice(0,1,msg);
- },
- // 删除消息提示
- delMsg(state, data) {
- // setTimeout(() => {
- // console.log('删除了');
- // }, 2000)
- state.msgpop.shift();
- },
- delAll(state, data) {
- state.msgpop = [];
- },
- // 保存发布内容
- // saveformData(state, formData) {
- // state.formData = formData;
- // },
- removeData(state) {
- state.formData = {};
- }
- },
- state: {
- //公共的变量,存储数据,这里的变量不能随便修改,只能通过触发mutations的方法才能改变
- user: {
- openid: '',
- avatar: '', //头像
- username: '', //用户名
- sex: '', //用户性别
- school: '', //学校
- spe: '', //专业
- rankl: '', //职称
- brief: '', //用户简介
- tel: "", //联系方式
- },
- msgpop: []
- // 聊天记录
- // message: [],
- // formData: {}
- },
- getters: {
- getUser(state) {
- return state.user;
- },
- getMsg(state) {
- return state.message;
- }
- },
- plugins: [persistedstate({
- // 传入参数
- storage: {
- getItem: (key) => uni.getStorageSync(key),
- setItem: (key, val) => uni.setStorageSync(key, val),
- removeItem: (key) => uni.removeStorageSync(key)
- }
- })]
- })
- export default store
|