1.严格模式

    import getters from './getters'
import mutations from './mutations'
import actions from './actions'
export default new Vuex.Store({
strict: true, //严格模式开启
state: { // 全局state
},
modules:{ // 外部模块
},
getters, // 全局getters
mutations, // 全局mutation
actions, // 全局actions
})
// 如果在vue页面中直接修改state 会报错
this.$store.state.namespace.stateName= '直接修改state'
// Error: [vuex] Do not mutate vuex store state outside mutation handlers.

this.$store 实例解析

// 在主入口文件main.js 或者 index.js 中,一旦引入并use了store实例后,
// 在new Vue({})之后,
// 便可以在任意vue文件中使用 this.$store来使用store中state/action/mutation
import store from './store'
Vue.use(store)
new Vue({
router,
store,
template: '<app/>',
components: { App }
}) // A.vue
console.info(this.$store)
/**
commit: ƒ (e,t,a)
dispatch: ƒ (e,t)
getters: {…} // 包含了在 new Vuex.Store({getters})的所有属性
strict: true
_actionSubscribers: []
_actions: // 包含全部的action, 全局的和module中的
_committing: false
_modules: c {root: s}
_modulesNamespaceMap: // 每个单独module的命名空间 /user, /dictionary, /list
_mutations: // 包含全部的mutations, 全局的和module中的
_subscribers: []
state: (...) // 包含全部的state, 全局的和module中的
**/

3. state

    // 直接调用 state
console.info('this.$store.state.user.flag:', this.$store.state.user.flag)

4. getters

    // 直接调用 getters(前提是在getter中声明了某个state), example: flag: state => state.user.flag,
console.info('this.$store.getters.flag:', this.$store.getters.flag)
// 会获取同样的值

5. dispatch 带有异步操作

    // dispatch执行的 action
// this.$store.dispatch('user/actionName') // demo
console.info(' ##### Before dispatch #####')
this.$store.dispatch('StartLoading')
console.info('this.$store.state.loading:', this.$store.state.loading) // true
this.$store.dispatch('EndLoading')
console.info(' ##### After dispatch #####')
console.info('this.$store.state.loading:', this.$store.state.loading) // false

6. commit 无异步操作

    // commit执行的是mutation
// this.$store.commit('namespace/mutationName') // demo
console.info(' ##### Before commit #####')
console.info('this.$store.state.user.flag:', this.$store.state.user.flag) // I am flag
console.info('this.$store.getters.flag:', this.$store.getters.flag) // I am flag
this.$store.commit('user/SET_FLAG', 'commit mutation to change state')
console.info(' ##### After commit #####')
console.info('this.$store.state.user.flag:', this.$store.state.user.flag) // commit mutation to change state
console.info('this.$store.getters.flag:', this.$store.getters.flag) // commit mutation to change state

Vue Vuex 严格模式+实例解析+dispatch/commit + state/getter的更多相关文章

  1. Vue Vuex中的严格模式/实例解析/dispatch/commit /state/getters

    严格模式 import getters from './getters' import mutations from './mutations' import actions from './acti ...

  2. 一文解析Pinia和Vuex,带你全面理解这两个Vue状态管理模式

    Pinia和Vuex一样都是是vue的全局状态管理器.其实Pinia就是Vuex5,只不过为了尊重原作者的贡献就沿用了这个看起来很甜的名字Pinia. 本文将通过Vue3的形式对两者的不同实现方式进行 ...

  3. 实例解析Python设计模式编程之桥接模式的运用

    实例解析Python设计模式编程之桥接模式的运用 这篇文章主要介绍了Python设计模式编程之桥接模式的运用,桥接模式主张把抽象部分与它的实现部分分离,需要的朋友可以参考下 我们先来看一个例子: #e ...

  4. Datatables插件1.10.15版本服务器处理模式ajax获取分页数据实例解析

    一.问题描述 前端需要使用表格来展示数据,找了一些插件,最后确定使用dataTables组件来做. 后端的分页接口已经写好了,不能修改.接口需要传入页码(pageNumber)和页面显示数据条数(pa ...

  5. use vue vuex vue-router, not use webpack

    vue,vuex,vue-router放在一起能做什么?不用webpack之类的打包工具使用他们是否可行?各位道友在初学vue时是否有这样的困惑.因为现代构建前端项目的一般模式是: 安装webapck ...

  6. vue+vuex初入门

    Vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 解决问题: 传参的方法对于多层嵌 ...

  7. [Vue] vuex进行组件间通讯

    vue 组件之间数据传输(vuex) 初始化 store src/main.js import Vuex from "vuex"; Vue.use(Vuex); new Vue({ ...

  8. vue+vuex+axios+echarts画一个动态更新的中国地图

    一. 生成项目及安装插件 # 安装vue-cli npm install vue-cli -g # 初始化项目 vue init webpack china-map # 切到目录下 cd china- ...

  9. vue + vuex 表单处理

    使用场景:在一个表单中,各项数据提交后需要重置表单中各<input>元素的值为空. 组件中关联: <template> <el-form ref="form&q ...

随机推荐

  1. zabbix自定义监控(当会话登录超过三个就报警)

    安装过程在此省略. 1.agent端去修改配置文件 2.调用自定义内容 vim /etc/zabbix/zabbix_agentd.d/login.conf UserParameter=login-u ...

  2. 一款直击痛点的优秀http框架,让我超高效率完成了和第三方接口的对接

    1.背景 因为业务关系,要和许多不同第三方公司进行对接.这些服务商都提供基于http的api.但是每家公司提供api具体细节差别很大.有的基于RESTFUL规范,有的基于传统的http规范:有的需要在 ...

  3. Zookeeper ----- ZAB算法

    介绍 Zookeeper没有使用Paxos实现,而是使用ZAB(Zookeeper原子消息广播协议)作为数据一致性的核心算法. ZAB是一种专为Zookeeper设计的支持崩溃恢复的原子广播协议. Z ...

  4. DeviceEventEmmiter使用

    发送广播一个事件 DeviceEventEmitter.emit('updatePlantList', '创建工厂成功');//通知刷新工厂列表 接收处,添加监听(监听要再事件发生之前添加,否则无法回 ...

  5. 修改map中原来的各种Key

    简单描述: 做数据迁移的时候,需要展示数据库的字段信息,但是我发现 Oracle的sql查询到的结果 出来默认是大写的 和 前端vue的参数小写开头+驼峰 不太一样 所以后台取到的数据都是是引用Lis ...

  6. Oracle整合Mybatis实现list数据插入时,存在就更新,不存在就插入以及随机抽取一条记录

    作者:故事我忘了¢个人微信公众号:程序猿的月光宝盒 目录 Oracle整合Mybatis实现list数据插入时,存在就更新,不存在就插入 entity 对应表中字段,如不对应,在xml中起别名 map ...

  7. lua判断字符串包含另一个字符串

    lua判断字符串包含另一个字符串 --string.find("元字符串","模式字符串") 如下: print(string.find("CCBWe ...

  8. PHP 函数实例讲解

    PHP 函数 PHP 的真正威力源自于它的函数. 在 PHP 中,提供了超过 1000 个内建的函数. PHP 内建函数 如需查看所有数组函数的完整参考手册和实例,请访问我们的 PHP 参考手册. P ...

  9. CF R 635 div1 C Kaavi and Magic Spell 区间dp

    LINK:Kaavi and Magic Spell 一打CF才知道自己原来这么菜 这题完全没想到. 可以发现 如果dp f[i][j]表示前i个字符匹配T的前j个字符的方案数 此时转移变得异常麻烦 ...

  10. JS时间和时间戳的转换

    时间转为时间戳 timeToTimestamp(time){ let timestamp = Date.parse(time) return timestamp; } 时间戳转为本地时间 timest ...