vue store getters mapGetters 具体用法

1、首先在src下面建一个store文件夹

具体的结构是:modules(文件夹) getter.js index.js

2、在main.js 里引入 

import store from './store'
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})

3、index.js 里面的代码

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

4、modules 里是各个模块需要用到的数据 各种js文件

const state = {
  followfileList: [], // 档案分组列表
  // followfileListLocal: [], // 本地分组列表
  countryList: [], // 国家列表
  cancelSinglebaseId: null, // 档案被取消关注的人员id
  homeRequestFinishNum: 0,
  specialList: [],//全部专题列表
  wordList: [],//热搜词列表
  rootSpecialList: [],//全部要闻专题
  organizationListAll: [],//智能档案-组织根名称
}

const mutations = {
  SAVE_FOLLOW_FILE_LIST: (state, list) => {
    state.followfileList = list
  },
  SAVE_COUNTRY_LIST: (state, list) => {
    state.countryList = list
  },
  SAVE_SPECIAL_LIST: (state, list) => {
    state.specialList = list
  },
  SAVE_WORD_LIST: (state, list) => {
    state.wordList = list
  },
  SAVE_ROOTSPECIAL_LIST: (state, list) => {
    state.rootSpecialList = list
  },
  UPDATE_CANCEL_SINGLE_BASEID: (state, baseId) => {
    state.cancelSinglebaseId = baseId
  },

  UPDATE_HOME_REQUEST_FINISH_NUM: (state) => {
    state.homeRequestFinishNum++
  },
  SAVE_ORGANIZATION_LIST_ALL: (state, list) => {
    state.organizationListAll = list
  },
}
export default {
  namespaced: true,
  state,
  mutations
}

5、放需要获取的字段

const getters = {
  token: state => state.user.token,
  name: state => state.user.name,
  user_id: state => state.user.user_id,
  dept_id: state => state.user.dept_id,
  role_type: state => state.user.role_type,
  menus: state => state.user.menus,
  categories: state => state.user.categories,
  routes: state => state.permission.routes,
  visitedViews: state => state.tagsView.visitedViews,
  cachedViews: state => state.tagsView.cachedViews,
  city_lat: state => state.user.city_lat,
  city_lon: state => state.user.city_lon,
  followfileList: state => state.sysData.followfileList,
  organizationListAll: state => state.sysData.organizationListAll,
  tilList:state=>state.reports.tilParams,
  categoryed:state => state.reports.category,
  categoryIds:state => state.reports.categoryIds
}

export default getters

// 报告订阅
const state = {
  tilParams: {},
  category: {},
  categoryIds:[]
}

const mutations = {
  SAVE_TIL_PARAMS: (state,data) => {
    state.tilParams = data
  },
  CATE_GORY: (state, data) => {
    state.category = data
  },
  SAVE_ALL_CATES:(state,data) => {
    state.categoryIds = data
  }
}
export default {
  namespaced: true,
  state,
  mutations
}

 

6、在需要使用的页面

首先要使用肯定要存储          reports 是js的文件名

this.$store.commit("reports/SAVE_TIL_PARAMS", til);

7、也是在需要使用的页面

import { mapGetters } from "vuex";
computed:{
 ...mapGetters(["tilList", "categoryed"]),
}

要使用这个数据的话

const tie = JSON.parse(this.tilList)

  • 19
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值