当前位置: 首页 > news >正文

vuex视频教程

参考:https://jspang.com/post/vuex.html

总结:不要过渡使用vuex,能使用参数传递,尽量使用参数传递。

 

1、Vuex基本介绍
数据仓库,状态管理器。
以前是用session操作。

学习基础:Router vue-cli
安装:$ npm install vuex --save

 

2、项目创建
创建基础项目
$ vue init webpack vuex
? Project name (vuex)
? Project name vuex
? Project description (A Vue.js project)
? Project description A Vue.js project
? Author (wang <weiwei@163.com>)
? Author wang <weiwei@163.com>
? Vue build (Use arrow keys)
? Vue build standalone
? Install vue-router? (Y/n) y
? Install vue-router? Yes
? Use ESLint to lint your code? (Y/n) n
? Use ESLint to lint your code? No
? Set up unit tests (Y/n) n
? Set up unit tests No
? Setup e2e tests with Nightwatch? (Y/n) n
? Setup e2e tests with Nightwatch? No
? Should we run `npm install` for you after the project has been created? (recom
? Should we run `npm install` for you after the project has been created? (recom
mended) npm

3、部署vuex环境
创建项目目录:vuex/store.js文件
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const state={
count: 0
}

//改变state值,必须调用mutations方法
const mutations={
add(state,n){
state.count+=n;
},
reduce(state){
state.count--;
},
reset(state){
state.count=0;
}
}

export default new Vuex.Store({
state
})


创建components/Count.vue组件。
<template>
<div class="hello">
<h1>{{ msg }}</h1><hr>
<h2>{{$store.state.count}}=====>{{count}}</h2>
<p>
<button @click="$store.commit('add',10)">+</button>
<button @click="reduce">-</button>
<button @click="$store.commit('reset')">reset</button>
</p>
</div>
</template>

<script>
import store from '@/vuex/store'
import { mapState,mapMutations ,mapGetters, mapActions} from 'vuex'

export default {
data () {
return {
msg: 'Hello Vuex !'
}
},
computed:{
...mapState(['count']),
},
store
}
</script>


配置路由
import Count from '@/components/Count'

Vue.use(Router)

export default new Router({
routes: [
{
path: '/',
component: HelloWorld
},{
path: '/count',
component: Count
}
]
})

访问方式:http://localhost:8080/#/count
点击“+”自增1,“-”自减1,RESET重置归零。

 

4、修改状态值
Getters,actions同步异步修改访问。
const getters={
count:function(state){
return state.count+=100;
}
}

//异步修改数据
const actions={
addAction(context){
context.commit('add',10);
//配置延迟3秒减1,测试。
setTimeout(()=>{context.commit('reduce')},3000);
console.log('我比reduce先执行了!')
},
reduceAction({commit}){
commit('reduce');
}
}
export default new Vuex.Store({
state,mutations,getters,actions
})

完善配置Count组件。
<template>
<div class="hello">
<h1>{{ msg }}</h1><hr>
<h2>{{$store.state.count}}=====>{{count}}</h2>
<p>
<button @click="$store.commit('add',10)">+</button>
<button @click="reduce">-</button>
<button @click="$store.commit('reset')">reset</button>
</p>
<p>
<button @click="addAction">+</button>
<button @click="reduceAction">-</button>
</p>
</div>
</template>

<script>
import store from '@/vuex/store'
import { mapState,mapMutations ,mapGetters, mapActions} from 'vuex'

export default {
data () {
return {
msg: 'Hello Vuex !'
}
},
computed:{
...mapState(['count']),
//...mapGetters(['count'])
},
methods:{
...mapMutations(['add','reduce']),
...mapActions(['addAction','reduceAction']),
},
store
}
</script>


至此,完成了vuex功能学习。

补充内容:
Action修改状态(异步修改)。
同步和异步的区别。

模块组
项目不复杂,不建议使用。
实际项目组尽量不要使用,可不纠结。

转载于:https://www.cnblogs.com/sunnyyangwang/p/10552223.html

相关文章:

  • 关于 +new Date 的个人见解
  • JOB SERVER 负载均衡
  • 如何保护云中的工作负载
  • mysql触发器的作用及语法
  • CSS 通用原子类
  • 二、 vSphere 6.7 U1(二):对Esxi主机设置
  • 基础面试题String、变量、类与对象、集合类、SSH
  • Android中Activity启动模式详解
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • Git 文件操作
  • 阶乘后的零[LeetCode-172]
  • 理解WebKit和Chromium: Android 4.4 上的Chromium WebView
  • springboot 统一异常处理(包含统一数据校验)
  • JavaSE-note1-随机访问类RandomAccessFile
  • git进阶之.gitignore 和 .gitmodules
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • [nginx文档翻译系列] 控制nginx
  • cookie和session
  • dva中组件的懒加载
  • javascript 总结(常用工具类的封装)
  • JavaScript对象详解
  • JS 面试题总结
  • js操作时间(持续更新)
  • JS题目及答案整理
  • Object.assign方法不能实现深复制
  • PhantomJS 安装
  • python 学习笔记 - Queue Pipes,进程间通讯
  • Spring框架之我见(三)——IOC、AOP
  • 从0实现一个tiny react(三)生命周期
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 搭建gitbook 和 访问权限认证
  • 机器学习 vs. 深度学习
  • 你不可错过的前端面试题(一)
  • 新书推荐|Windows黑客编程技术详解
  • 移动端解决方案学习记录
  • Mac 上flink的安装与启动
  • 格斗健身潮牌24KiCK获近千万Pre-A轮融资,用户留存高达9个月 ...
  • 我们雇佣了一只大猴子...
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • #免费 苹果M系芯片Macbook电脑MacOS使用Bash脚本写入(读写)NTFS硬盘教程
  • $$$$GB2312-80区位编码表$$$$
  • (2020)Java后端开发----(面试题和笔试题)
  • (Arcgis)Python编程批量将HDF5文件转换为TIFF格式并应用地理转换和投影信息
  • (C语言)共用体union的用法举例
  • (多级缓存)缓存同步
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (七)理解angular中的module和injector,即依赖注入
  • (实战)静默dbca安装创建数据库 --参数说明+举例
  • (学习日记)2024.03.25:UCOSIII第二十二节:系统启动流程详解
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • (转)Sql Server 保留几位小数的两种做法
  • (转)大型网站的系统架构
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...