Vuex 的使用 State Mutation Getter Action
import Vue from 'vue'
import Vuex from 'vuex' Vue.use(Vuex); /*1.state在vuex中用于存储数据*/
var state={ count:1,
list:[]
} /*2.mutations里面放的是方法,方法主要用于改变state里面的数据
*/
var mutations={ incCount(){ ++state.count;
},
addList(state,data){ state.list = data;
}
} /*3、优点类似计算属性 , 改变state里面的count数据的时候会触发 getters里面的方法 获取新的值 (基本用不到)*/ var getters= { computedCount: (state) => {
return state.count*2
}
} /*
4、 基本没有用 Action 类似于 mutation,不同在于: Action 提交的是 mutation,而不是直接变更状态。
Action 可以包含任意异步操作。
*/ var actions= {
incMutationsCount(context) { /*因此你可以调用 context.commit 提交一个 mutation*/ context.commit('incCount'); /*执行 mutations 里面的incCount方法 改变state里面的数据*/ }
} //vuex 实例化 Vuex.store 注意暴露
const store = new Vuex.Store({
state,
mutations,
getters,
actions
}) export default store;
<template>
<!-- 所有的内容要被根节点包含起来 -->
<div id="home" style="padding:20px;">
我是首页组件 -- {{this.$store.state.count}} ----{{this.$store.getters.computedCount}} <button @click="incCount()">增加数量+</button> </div>
</template> <script> //1. 引入store 建议store的名字不要变 import store from '../vuex/store.js'; //2.注册
export default{
data(){
return {
msg:'我是一个home组件',
value1: null, }
},
store,
methods:{
incCount(){
//改变vuex store里面的数据 //this.$store.commit('incCount'); /*触发 mutations 改变 state里面的数据*/ this.$store.dispatch('incMutationsCount'); /*触发 actions里面的方法 */
}
}
} </script> <style lang="scss" scoped> </style>
<template>
<div id="news">
我是新闻组件 --{{this.$store.state.count}} <br> <button @click="incCount()">增加数量</button> <br><br>
<br><br> <ul>
<li v-for="item in list"> {{item.title}}
</li>
</ul> </div> </template> <script>
//1. 引入store import store from '../vuex/store.js'; export default{
data(){
return {
msg:'我是一个新闻组件',
list:[] }
},
store,
methods:{ incCount(){ this.$store.commit('incCount');
}, requestData(){ //请求数据 var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1'; this.$http.get(api).then((response)=>{
console.log(response); //注意this指向 this.list=response.body.result; //数据放在store里面 this.$store.commit('addList',response.body.result); },function(err){ console.log(err); })
}
},mounted(){ //判断 store里面有没有数据
var listData=this.$store.state.list; console.log(listData.length); if(listData.length>0){
this.list=listData; }else{ this.requestData(); } }
} </script> <style lang="scss" scoped> .list{ li{
height:3.4rem; line-height:3.4rem; boder-bottom:1px solid #eee; font-size:1.6rem; a{ color:#666; }
}
} </style>
Vuex 的使用 State Mutation Getter Action的更多相关文章
- 挑战全网最幽默的Vuex系列教程:第二讲 Vuex旗下的State和Getter
先说两句 上一讲 「Vuex 到底是个什么鬼」,已经完美诠释了 Vuex 的牛逼技能之所在(纯属自嗨).如果把 Vuex 比喻成农药里面的刘备,那就相当于你现在已经知道了刘备他是一个会打枪的力量型英雄 ...
- vuex mutation,action理解
1. 在store中分别注册mutation和action,action中用commit同步调用mutation来执行修改state,但是在组件中则使用dispatch异步调用action 2. 通俗 ...
- vuex2.0 基本使用(2) --- mutation 和 action
我们的项目非常简单,当点击+1按钮的时候,count 加1,点击-1按钮的时候,count 减1. 1, mutation The only way to actually change state ...
- 【14】vuex2.0 之 mutation 和 action
我们的项目非常简单,当点击+1按钮的时候,count 加1,点击-1按钮的时候,count 减1. 1, mutation The only way to actually change state ...
- (转)vuex2.0 基本使用(2) --- mutation 和 action
我们的项目非常简单,当点击+1按钮的时候,count 加1,点击-1按钮的时候,count 减1. 1, mutation The only way to actually change state ...
- vuex 源码解析(三) getter属性详解
有时候我们需要从store中的state中派生出一些状态,例如: <div id="app"> <p>{{reverseMessage}}</p> ...
- vue常见问题处理 -- 页面刷新时,如何保持原有vuex中的state信息
一.页面刷新时,如何保持原有vuex中的state信息 页面刷新后,原有的 vuex 中的 state 会发生改变,如果在页面刷新之前,可以将 state 信息保存,页面重新加载时,再将该值赋给 st ...
- vuex 之既生‘mutation’何生‘action’
vuex 中,action 及 mutation 均为操作数据的作用而存在,既然二者均可改变数据,为什么要分成两个方法来处理呢,因为: Mutation 必须是同步函数 mutations: { so ...
- vuex中mutation和action的详细区别
const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment (state) { state.count++ } ...
随机推荐
- Bicoloring (并查集/二分图)
题目链接 题意: m个查询,每个查询输入a b,表示 顶点a b之间涂色. 规定只能涂颜色0 或者颜色 1,一个节点相连的边 必须涂成相同的颜色. 问 ,输入m组 a b之后,会不会犯规. 思路: 判 ...
- java基础(9)---静态方法和成员方法
一.方法: 方法的区别: 静态方法:有static方法 成员方法:没有static方法 方法的定义: 方法的调用:类.静态方法,对象.成员方法 一个MyClass类包含静态方法和成员方法: 静态方 ...
- LG2664 树上游戏
树上游戏 题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 $$sum_i=\sum_{j=1}^ns(i,j)$$ 现在他想让 ...
- sql:拼接字符串、截取字符串、取字符串长度
--第一段 SELECT substr('1233***6795', 0, (select instr('1233***6795', '***', 1, 1) from dual) - 1) from ...
- wait,waitpid学习测试
用man wait学习wait waitpid的使用 wait()函数功能:wait()函数使父进程暂停执行,直到它的一个子进程结束为止,该函数的返回值是终止运行的子进程的PID. 参数status所 ...
- 软件测试技术之可用性测试之WhatsApp Web
Tag:可行性测试.测试流程.结果分析.案例分析 WhatsApp是一款面向智能手机的网络通讯服务,它可以通过网络传送短信.图片.音频和视频.WhatsApp在全球范围内被广泛使用,是最受欢迎的即时聊 ...
- JS AJAX和JSONP的基础功能封装以及使用示例;
1.代码: function ajax(options){ options = options || {}; options.type = options.type || "get" ...
- 1.5 synchronized其他概念
synchronized锁重入: 关键字synchronized拥有锁重入的功能,也就是使用synchronized时,当一个线程得到了一个对象的锁后,再次请求此对象时是可以再次得到对象的锁. 输出结 ...
- XAMPP环境搭建WordPress,DVWA
本周学习内容: 1.学习MySQL数据库.Linux.PHP开发: 2.复习等级培训内容: 3.使用xampp环境安装WordPress,学习WordPress数据库表的设计: 4.使用xampp安装 ...
- Problem 1 bfs+set
$des$ 小G有一个长度为 $n$ 的 01 串 T ,其中只有 $T_S = 1$,其余位置都是 $0$.现在小G可以进行若干次以下操作:选择一个长度为 $K$ 的连续子串(K是给定的常数),翻转 ...