美文网首页
Vue使用vuex进行项目模块化,两种调用方式

Vue使用vuex进行项目模块化,两种调用方式

作者: 程序元客栈 | 来源:发表于2022-05-06 09:12 被阅读0次

刚开始接触uniApp看了一下vuex的使用方法,总结一下在项目中个人认为常用的架构,很实用,做一个新手学习笔记!!!

文件目录结构图

目录结构图

创建store目录

使用Vue创建项目,在项目内创建store目录,在目录内创建index.js文件,如下

namespaced : true 调用时候 需要加上所属的模块名可以参数第二种调用方式

import Vue from 'vue'
import Vuex from 'vuex'
import star_sky from './modules/public/star_sky.js'
Vue.use(Vuex)

const store = new Vuex.Store({

    state: {
        //放置全局变量
    },

    modules: {
        //数据模块化
        star_sky: {
            namespaced: true,
            ...star_sky
        }
    }

})
export default store

在store内创建mutations_type.js

mutations_type.js 是方便异步方法actions 调用同步方法mutations 去更新数据时调用,不容易出错而且模块化后方法是通过所属模块名称去调用

//页面请求实体数据
export const REQUEST_DATA = 'REQUEST_DATA' 

store目录内创建modules

modules 是各个模块对应的文件

modules 内创建公共模块文件夹

public内都是多个UI共享数据,创建star_sky.js 文件

创建star_sky.js

import { REQUEST_DATA } from '../../mutations_type.js' 
const state = {
  ceshidata: []
}

const mutations = {
  [REQUEST_DATA] (state, data) {
    state.ceshidata = data
   }
}
const actions = {
  async getDevicesData ({ commit },data) {
    console.log("UI 界面调用 异步actions 内方法")
         // 异步操作 ---请求数据
              
       //同步操作 更新 ceshidata 数据        
         commit(REQUEST_DATA, data)
  }
}

export default {
  state,
  mutations,
  actions
}

在main.js中配置store

import store from 'store/index.js'
Vue.prototype.$store = store
const app = new Vue({
...App,
store
})

调用模块

方式一
  • mapState 是引用模块内state内的数据
  • mapActions 是引用模块内actions 内的方法 (异步)
  • mapGetters 是引用模块内 getters 内的方法 (获取数据)
  • mapMutations 是引用模块内 mutations 的方法 (同步修改state)
  • 在vue script 内引入vuex
import { mapState, mapActions } from 'vuex'
  • mapState
    namespace:true 需要加上模块名称 ,在export default 内 创建 computed 导出 模块 mapState 方法
computed: {
            ...mapState('star_sky', ['ceshidata'])
        }
  • mapActions
    namespace:true 需要加上模块名称 ,在export default 内 创建 methods导出 模块 mapActions方法
methods: {
            ...mapActions('star_sky', ['getDevicesData'])
        }
  • mapGetters
    namespace:true 需要加上模块名称 ,在export default 内 创建 computed 导出 模块 mapGetters 方法
computed: {
            ...mapGetters('star_sky', ['方法名'])
        }
  • mapMutations
    namespace:true 需要加上模块名称 ,在export default 内 创建 methods导出 模块 mapMutations方法
methods: {
            ...mapMutations('star_sky', [REQUEST_DATA])
        }
方式二

这都是建立在namespace:true的前提下,如果namespace:false 则去掉模块名

  • mapState 是引用模块内state内的数据
  • mapActions 是引用模块内actions 内的方法 (异步)
  • mapGetters 是引用模块内 getters 内的方法 (获取数据)
  • mapMutations 是引用模块内 mutations 的方法 (同步修改state)
  • mapState

结构:this.$store.state.模块名.模块属性

this.$store.star_sky.ceshidata
  • mapActions

结构:this.$store.dispatch('模块名 / 模块中的actions',值)

this.$store.dispatch('star_sky/getDevicesData',[{'ceshikey': '第二种方法'}])
  • mapGetters

结构:this.$store.getters.模块名.模块属性

//this.$store.getters.star_sky.doneTodosCount
this.$store.getters.模块名.模块属性
  • mapMutations

结构:this.$store.commit('模块名 / 模块中的mutations',值)

this.$store.commit('star_sky/模块中的mutations',[{'ceshikey': '第二种方法'}])

相关文章

  • Vue使用vuex进行项目模块化,两种调用方式

    刚开始接触uniApp看了一下vuex的使用方法,总结一下在项目中个人认为常用的架构,很实用,做一个新手学习笔记!...

  • uniapp使用vuex进行项目模块化,两种调用方式

    刚开始接触uniApp看了一下vuex的使用方法,总结一下在项目中个人认为常用的架构,很实用,做一个新手学习笔记!...

  • vuex使用和规范

    关于vuex使用和规范vuex示例 在vue页面中调用和使用分两种情况,第一种直接使用,使用computed获取,...

  • vue-cli3.0 + Vuex + typescript:M

    vue+typescript之后,vuex调用再也不是从前的调用方式了,需要加类型校验了 对比vuex在使用ts前...

  • 2019-01-19

    Vue+Vuex+axios+sass项目: 抽空整理了一下使用vue-cli创建vue项目并在项目中使用Vuex...

  • 使用 node 模拟请求接口

    上一篇:Vuex 进阶——模块化组织 Vuex 使用 Vue 写项目肯定会遇到一个问题,如何模拟服务端请求数据,那...

  • Vue优雅使用技巧(二)--vuex细节使用

    Vue中vuex使用 vue的项目只要拆分出来一定要做到 模块化处理,不管是路由,数据状态,组件,混入,指令,过滤...

  • 2018-04-03

    vuex(三) vuex文档给了如下案例: 案例中提示如果需要使用vuex的模式需要先调用:Vue.use(Vue...

  • 2018-01-13

    Vue 用模块化的方式构建项目 想要提高代码的复用性,vue 项目中可以使用插件的方法: MyPlugin.in...

  • 2018-01-13

    Vue 用模块化的方式构建项目 想要提高代码的复用性,vue 项目中可以使用插件的方法: MyPlugin.ins...

网友评论

      本文标题:Vue使用vuex进行项目模块化,两种调用方式

      本文链接:https://www.haomeiwen.com/subject/dmpayrtx.html