Vue Vuex state mutations
Vuex
解决不同组件的数据共享,与数据持久化
1.npm install vuex --save
2.新建store.js 并引入vue vuex ,Vue.use(Vuex)
3.state在vuex中用于存储数据
var state = {
count:1
}
4.mutations里放的是方法,主要用于改变state中的数据
var mutations = {
incCount(){
++state.count;
}
}
5.实例化vuex.Store
consta store = new Vuex.Store({
state,
mutations
})
6.export default store;
7.组件A中引入store
import store from '../store.js'
8.注册
mounted(){},
store
9.
通过this.$store.state.count引用属性
通过this.$store.commit.('incCount'))引用方法
10.getters类似于计算属性,改变state里面的count数据的时候,触发getters里的方法,获取新的值
var getters = {
computedCount : (state)=>{
return state.count*2
}
}
通过 this.$store.getters.computedCount调用
11.Action类似于mutation,不同在于Action提交的是mutation,而不是直接改变状态。Action可包含任意异步操作
var actions = {
incMutationsCount(context){
context.commit('incCount') /*执行mutations里的incCount方法*/
}
}
通过this.$store.dispatch('incMutationsCount')调用
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex); var state = {
count:
} const mutations = {
run(){
++state.count;
}
} const store = new Vuex.Store({
state,
mutations
}); export default store;
<template>
<div id="app">
<router-link to="/home">Home组件</router-link>
<router-link to="/news">News组件</router-link>
<hr>
<router-view></router-view>
</div>
</template> <script>
export default {
name: 'app',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script> <style lang="scss"> </style>
<template>
<div id="news">
News组件
{{this.$store.state.count}}
</div>
</template> <script>
import store from '../utils/store.js'
export default {
data() {
return{}
},
store
};
</script>
<template>
<div id="home">
Home组件
{{this.$store.state.count}}
<br>
<button @click="addstate()">添加state</button>
</div>
</template> <script>
import store from '../utils/store.js'
export default {
data() {
return{}
},
store,
methods:{
addstate(){
this.$store.commit('run')
}
}
};
</script>
Vue Vuex state mutations的更多相关文章
- VUE - vuex state的使用
1,安装 进入项目目录,执行 vue add vuex 命令 2,会在src的目录下新增store文件夹 3,打开store文件夹下的index.js , 给 state 设定一些数据 impor ...
- [Nuxt] Update Vuex State with Mutations and MapMutations in Vue.js
You commit changes to state in Vuex using defined mutations. You can easily access these state mutat ...
- 在vuex的mutations中使用vue的小技巧
问题: 在vuex组件中的mutations属性中的定义的函数,有时会要用到vue这个对象.正常在其他的地方使用是通过this这个变量来获取,但是在mutations定义的函数中this指定的是Vue ...
- vue自学入门-5(vuex state)
vue自学入门-1(Windows下搭建vue环境) vue自学入门-2(vue创建项目) vue自学入门-3(vue第一个例子) vue自学入门-4(vue slot) vue自学入门-5(vuex ...
- vue 关于deep watch / computed 监听不到 vuex state 对象变化的的问题
简而言之,如果vuex state 中是一个对象 {},那么监听就会有问题.先给出解决方案: // 超简易拷贝(如果是深拷贝还多此一举把get/set拷贝进去了,所以用简易拷贝即可) let __VA ...
- vue+vuex初入门
Vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 解决问题: 传参的方法对于多层嵌 ...
- use vue vuex vue-router, not use webpack
vue,vuex,vue-router放在一起能做什么?不用webpack之类的打包工具使用他们是否可行?各位道友在初学vue时是否有这样的困惑.因为现代构建前端项目的一般模式是: 安装webapck ...
- vue vuex的用法
1.引入 vue.js vuex.js 文件 2.创建Store文件 var sSatte=new Vuex.Store({ state:{}, mutations:{}, actions:{ ...
- Vuex state 状态浅解
对于Vuex中的state里面的理解总是有些欠缺,机制似乎理解了.但是还有很多的不足,在这就先浅谈下自己的理解. vuex 机制中,定义了全局Store,在各个vue组件面的this.$store指向 ...
随机推荐
- Quartz.net 2.x 学习笔记03-使用反射加载定时任务
将定时任务信息存储在XML文件中,使用反射加载定时任务 首先新建一个MVC的空站点,使用NuGet添加对Quartz.net和Common.Logging.Log4Net1213的引用,同时使用NuG ...
- apache 2 修改虚拟目录
准备好环境,就要开始进行开发了.这一篇,我们在Ubuntu Apache上配置虚拟目录. 知识准备: 区别于Windows 下apache,配置文件通常只有一个,就是httpd.conf. Linux ...
- C++11 类的六个默认函数及其使用
六个默认函数: 构造函数(construct) 析构函数(destruct) 复制构造函数(copy construct) 赋值(assign) 移动构造函数(move construct) 移动赋值 ...
- sourceTree免密码校验
1.ssh请求:参考:http://www.ithao123.cn/content-1584888.html 步骤1:检查你的电脑上是否已经生成了SSH Key 在git bash下执行如下命令 cd ...
- Flask 数据库多对多关系
数据库使用关系建立记录之间的联系.其中,一对多关系是最常用的关系类型,它把一个记录和一组相关的记录联系在一起.实现这种关系时,要在“多”这一侧加入一个外键,指向“一”这一侧联接的记录.大部分的其他关系 ...
- div+css 左右两列自适应高度 ,以及父级div也跟着自适应子级的高度(兼容各大浏览器)
<style type="text/css" media="screen"> <!-- #main {width:500px;_height: ...
- 一只小蜜蜂(斐波那契dp)
有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. Input输入数据的第一行是一个整数N,表示测试实例的个数,然后是 ...
- Effective ObjectiveC 2.0 Note
[Effective ObjectiveC 2.0 Note] 1.The memory for objects is always allocated in heap space and never ...
- java定时任务实现的几种方式(Timer、Spring Task、Quartz)
Timer JDK自带的Timer类,允许调度一个TimerTask任务. Demo: /** * Timer测试类 */ public class TimerDemo { public static ...
- 中国大学MOOC 玩转AutoCAD 熟悉AutoCAD的人机交互方式