严格模式

    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中的
**/

state

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

getters

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

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

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/getters的更多相关文章

  1. Vue Vuex 严格模式+实例解析+dispatch/commit + state/getter

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

  2. C# 动态生成word文档 [C#学习笔记3]关于Main(string[ ] args)中args命令行参数 实现DataTables搜索框查询结果高亮显示 二维码神器QRCoder Asp.net MVC 中 CodeFirst 开发模式实例

    C# 动态生成word文档 本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正. 在工程中引用word的动态库 在项目中,点击项目名称右键-- ...

  3. 如何去除vue项目中的 # --- History模式

    来自:https://www.cnblogs.com/zhuzhenwei918/p/6892066.html 侵删 使用vue-cli搭建的环境,在配置好路由之后,可以看到下面的情况: 但是不难发现 ...

  4. PHP中“简单工厂模式”实例讲解

    原创文章,转载请注明出处:http://www.cnblogs.com/hongfei/archive/2012/07/07/2580776.html 简单工厂模式:①抽象基类:类中定义抽象一些方法, ...

  5. mysql中limit的用法实例解析

    mysql中limit的用法解析. 在mysql中,select * from table limit m,n.其中m是指记录开始的index,从0开始,n是指从第m条开始,取n条. 例如: mysq ...

  6. Asp.net MVC 中 CodeFirst 开发模式实例

    昨天写的这篇博客因为下班时间到了忘记保存了,好郁闷,得重新写一遍.实习所在公司使用的是CodeFirst开发模式,最近开始参与到公司的项目里面来了,发现这个模式特别好用,建库建表改变字段属性添加删除字 ...

  7. vuex中strict严格模式

    开启严格模式,仅需在创建 store 的时候传入strict: true const store = new Vuex.Store({ state, strict:true//开启严格模式后,只能通过 ...

  8. PHP中“简单工厂模式”实例讲解(转)

      ? 1 2 3 4 5 6 7 8 原创文章,转载请注明出处:http://www.cnblogs.com/hongfei/archive/2012/07/07/2580776.html   简单 ...

  9. python中super的用法实例解析

    概念 super作为python的内建函数.主要作用如下: 允许我们避免使用基类 跟随多重继承来使用 实例 在单个继承的场景下,一般使用super来调用基类来实现: 下面是一个例子: class Ma ...

随机推荐

  1. P3379 最近公共祖先(LCA) 洛谷

    题意简单明了(这就是个模板). 就是让我们找2个节点的公共祖先而已,但我们要讲的做法不是生硬的爆搜,而且直接搜好像过不去…… 这次就讲我往后拖了n多天才开始学了倍增LCA. 嗯,这个题,如果2个节点的 ...

  2. window下远程连接redis服务

    首先下redis包: 下载地址:https://github.com/MSOpenTech/redis/releases. 之后: 1.注释掉redis.windows-service.conf 中的 ...

  3. 什么是viewstate,能否禁用?是否所用控件都可以禁用

    viewstate用于在两次postback之间保持状态的一种机制禁用viewstate将不能在回发之间保存状态 当控件状态无关使用viewstate将造成性能问题时需要禁用viewstate Vie ...

  4. 3c数码商城

    目标:2020样卷 已完成:增删改查 未完成:有些小知识点不在意丢失,因此导致有些未完善 解决方案:写代码时不要走心,专心一点,减少失误,减少时间,增加效率,使自己的项目看起来更优秀,注意小知识的掌握 ...

  5. 题解 洛谷 P4171 【[JSOI2010]满汉全席】

    考虑\(2-SAT\). 将汉式看作\(0\)状态,满式看做\(1\)状态,将每个材料拆成\(01\)两个状态. 从\(a\)向\(b\)连有向边表示的意义为选了\(a\)后必须选\(b\). 那么每 ...

  6. Mybatis(四)多表操作

    数据库如下: 一.创建数据库所对应的bean类 public class User { private Integer uId; private String username; private St ...

  7. 自动化不知如何参数化?xlrd来帮你解决

    平时在做自动化测试的时候,一直都是要求数据与业务逻辑分离.把测试数据都写在业务里面的话,比较混杂.为了方便管理测试数据,所以引入了python的一个扩展库--xlrd.该库使用简单,能满足自动化测试的 ...

  8. oracle 误删除 恢复

    select * from LFS_WELFAxxxxxASTDAYS AS OF TIMESTAMP  (SYSTIMESTAMP - INTERVAL '100' MINUTE) order by ...

  9. for循环运用,三角形

    用for循环打出三角形.倒三角形.金字塔.99乘法表 三角形: 打出如图三角形,分析行数与*个数的关系,用for循环 for(var i=0;i<5;++i){//i表示行数 var str=& ...

  10. 一切皆组件的Flutter,安能辨我是雄雌

    从一开始接触Flutter,相信读者都会铭记一句话,那就是--一切皆组件.今天我们就来体会一下这句话的神奇魔力,我们先从实际的产品需求说起. 我们先来看一个简化的运行图: 我们要实现如上图所示的日期选 ...