vue store存储commit和dispatch
vue store存储commit和dispatch
this.$store.commit('toShowLoginDialog', true);
this.$store.dispatch('toShowLoginDialog',false)
主要区别是:
dispatch:含有异步操作,例如向后台提交数据,写法: this.$store.dispatch('mutations方法名',值)
commit:同步操作,写法:this.$store.commit('mutations方法名',值)
例如:登录成功后读取用户信息写到coikie里
===============
Vue的项目中,如果项目简单, 父子组件之间的数据传递可以使用 props 或者 $emit 等方式 进行传递
但是如果是大中型项目中,很多时候都需要在不相关的平行组件之间传递数据,并且很多数据需要多个组件循环使用。
这时候再使用上面的方法会让项目代码变得冗长,并且不利于组件的复用,提高了耦合度。
Vue 的状态管理工具 Vuex 完美的解决了这个问题。
npm install vuex --save
然后在src目录下新建文件夹store,在文件夹内新建store.js文件,这个文件我们用来组装模块并导出 store 的文件
在main.js文件中注册store
import store from './store/store'
new Vue({
el: '#app',
router,
store,
template: '<App/>',
components: { App }
})
vue store存储commit和dispatch的更多相关文章
- vue-learning:41 - Vuex - 第二篇:const store = new Vue.Store(option)中option选项、store实例对象的属性和方法
vuex 第二篇:const store = new Vue.Store(option)中option选项.store实例对象的属性和方法 import Vuex from 'vuex' const ...
- 【vue store的使用方法】(this.$store.state this.$store.getters this.$store.dispatch this.$store.commit)
vue 页面文件 <template> <div> {{this.$store.state.count}}<br/> {{count}}<br/> {{ ...
- vue——store全局存储
业务场景:刷新页面时,首次拉取所有配置,存储到store状态管理用于全局调用: import Vue from 'vue' import Vuex from 'vuex' import userInf ...
- Vue+Electron下Vuex的Dispatch没有效果的解决方案
这个问题是解决基于 vue 和 electron 的开发中使用 vuex 的 dispatch 无效的问题,即解决了 Please, don't use direct commit's, use di ...
- vue 本地存储数据 sessionStorage
在vuex 下的 action下的userAction.js中添加 export function login(from, self) { axPost('/api/login', from, fun ...
- 使用vuex中的store存储数据
Vuex是一个专门为Vue.js应用程序开发的状态管理模式,这个状态自管理应用包括三个模式 state 驱动应用的数据源 view 以声明方式将state映射到视图 actions 响应在view上的 ...
- 如何实现Vue已经弃用的$dispatch和$broadcast方法?
对于父子(含跨级)传递数据的通信方式,Vue.js 并没有提供原生的 API 来支持,而是推荐使用大型数据状态管理工具 Vuex,但 Vuex 对于小型项目来说用起来真的很麻烦. 在 Vue.js 1 ...
- vue store获取值时 Computed property "activeTag" was assigned to but it has no setter.
出现原因: element-ui中 el-tab绑定的值在切换tab时会自动修改 而activeTag是从store中获取的值,不能直接修改 要添加给它绑定上set <el-tabs cla ...
- vue.js存储--localStorage
//list例子:绑定从localStorage中读取的数据,动态添加list并监听将数据变化存储在localStorage中,绑定点击事件改变样式, 页面 data数据: input_name:'' ...
随机推荐
- iOS 使用xib定义一个View,修改frame无效问题解决
遇到过好多次使用自定义view,修改frame无效问题, 之前都是放弃xib,直接手写,发现手写简单的还行,复杂的UI就坑逼了.所以还是需要用到可视化编辑的xib. 整理一下,自己备忘也供iOS开发的 ...
- 【Dubbo 源码解析】05_Dubbo 服务发现&引用
Dubbo 服务发现&引用 Dubbo 引用的服务消费者最终会构造成一个 Spring 的 Bean,具体是通过 ReferenceBean 来实现的.它是一个 FactoryBean,所有的 ...
- Non-zero exit code (1)
刚报了这个错Non-zero exit code (1) 经排查执行这个命令就好了 python -m pip install --upgrade pip 一定要多看报错,报错中有提示的
- Tensorflow Seq2seq attention decode解析
tensorflow基于 Grammar as a Foreign Language实现,这篇论文给出的公式也比较清楚. 这里关注seq2seq.attention_decode函数, 主要输入 de ...
- java后端实习,从最简单的crud做起
现在就是做ssm框架下的sql语句,主要是select语句,sql语句没什么难的,孰能生巧,趁此机会,把自己的sql基础打扎实,也是一种实习的经验. 1.在子查询中字段的类型不相容怎么办? cast函 ...
- 第四天,通过windows来执行第一个python文件步骤
该看 第 38部分的啦
- zhenya moves from parents
Zhenya moved from his parents' home to study in other city. He didn't take any cash with him, he onl ...
- profile和bashrc四种的区别
Linux下profile和bashrc四种的区别 12160阅读 0评论 /etc/profile./etc/bashrc.~/.bash_profile.~/.bashrc很容易混淆,他们之间有什 ...
- 2016 Multi-University Training Contest 2题解报告
A - Acperience HDU - 5734 题意: 给你一个加权向量,需要我们找到一个二进制向量和一个比例因子α,使得|W-αB|的平方最小,而B的取值为+1,-1,我们首先可以想到α为输入数 ...
- DBMS_METADATA.set_transform_param格式化输出
DBMS_METADATA.set_transform_param格式化输出获得DDL --输出信息采用缩排或换行格式化 EXEC DBMS_METADATA.set_transform_param( ...