Vuex基础

https://vuex.vuejs.org/zh-cn

state --> view --> action -> state

多组件共享状态, 之前操作方式,由父组件传递到各个子组件。 当路由等加入后,会变得复杂。 引入viewx 解决共享问题。

原vue结构图

vuex结构图

Vuex对象结构 (state,mutations,getters,actions)

state 对象数据

mutations 操作变更state数据

getters 计算state

actions  触发mutations

★学习之后分析数据流向图★

安装

npm install --save vuex

调试

目标

代码1  :原vue实现计数器

app.uve

<template>
<div><p>点击次数{{count}}, 奇偶数:{{eventOrOdd}}</p><button @click="increment">+</button><button @click="decrement">-</button><button @click="incrementIfOdd">奇数加</button><button @click="incrementAsync">异步加</button>
</div>
</template><script>
export default {data () {return {count: 0}},computed: {eventOrOdd () {return this.count % 2 === 0 ? '偶数' : '奇数'}},methods: {increment () {const count = this.countthis.count = count + 1},decrement () {const count = this.countthis.count = count - 1},incrementIfOdd () {const count = this.countif (count % 2 === 1) {this.count = count + 1}},incrementAsync () {setTimeout(() => {const count = this.countthis.count = count + 1}, 1000)}}
}
</script><style></style>

View Code

代码2: VUEX实现

store.js

/*** Created by infaa on 2018/9/22.*/
import Vue from 'vue'
import Vuex from 'vuex'Vue.use(Vuex)const state = { // 初始化状态count: 0
}
const mutations = {INCREMENT (state) {state.count++},DECREMENT (state) {state.count--}
}const actions = {increment ({commit}) {commit('INCREMENT')},decrement ({commit}) {commit('DECREMENT')},incrementIfOdd ({commit, state}) {if (state.count%2 === 1) {commit('INCREMENT')}},incrementAsync ({commit}) {setTimeout( () => {commit('INCREMENT')}, 1000)}
}const getters = {eventOrOdd (state) {return state.count % 2 === 0 ? '偶数' : '奇数'}
}export default new Vuex.Store({state, // 状态对象mutations, // 更新state函数的对象actions,// dispatch 对应actionggetters  // 对应computed 中getters
})

View Code

main.js

/*** Created by infaa on 2018/9/19.*/
import Vue from 'vue'
import App from './App'
import store from './store'/* eslint-disable no-new */new Vue({el: '#app',components: {App},template: '<App/>',store // 所有组件对象多了一个属性$store
})

View Code

app.vue

<template>
<div><!--<p>点击次数{{count}}, 奇偶数:{{eventOrOdd}}</p>--><p>点击次数{{$store.state.count}}, 奇偶数:{{eventOrOdd}}</p><button @click="increment">+</button><button @click="decrement">-</button><button @click="incrementIfOdd">奇数加</button><button @click="incrementAsync">异步加</button>
</div>
</template><script>
export default {
//  data () {
//    return {
//      count: 0
//    }
//  },computed: {eventOrOdd () {
//      return this.count % 2 === 0 ? '偶数' : '奇数'return this.$store.getters.eventOrOdd // js中要写this,模版中不用直接写$store}},methods: {increment () {this.$store.dispatch('increment')},decrement () {
//      const count = this.count
//      this.count = count - 1this.$store.dispatch('decrement')},incrementIfOdd () {this.$store.dispatch('incrementIfOdd')
//      const count = this.count
//      if (count % 2 === 1) {
//        this.count = count + 1
//      }},incrementAsync () {this.$store.dispatch('incrementAsync')
//      setTimeout(() => {
//        const count = this.count
//        this.count = count + 1
//      }, 1000)}}
}
</script><style></style>

View Code

代码3 优化app.js

app.uve 如果对应名不同,由[ ] 改为{}即可

<template>
<div><!-- <h2>点击的个数:{{count}}</h2> --><!-- <h3>{{eventOrOdd}}</h3> --><!-- Vuex 版本  --><h2>点击次数{{$store.state.count}}, 奇偶数:{{eventOrOdd}}</h2><button @click="increment">+</button><button @click="decrement">-</button><button @click="incrementIfOdd">奇数+</button><button @click="incrementAsync">异步加</button>
</div>
</template><script type="text/ecmascript-6">
import {mapState, mapGetters, mapActions} from 'vuex'// export default{//     data(){//         return{//             count:0//         }//     },//     computed:{//         eventOrOdd(count){//         //     return this.count % 2 === 0?'偶数':'奇数'//             return this.$store.getters.eventOrOdd//         }//     },//     methods:{//         increment () {//             // const count = this.count;//             // this.count = count+1//             this.$store.dispatch('increment')//         },//         decrement (){//             // const count = this.count;//             // this.count = count -1 //             this.$store.dispatch('decrement')//         },//         incrementIfOdd () {//             // const count = this.count;//             // if(count % 2 === 1 ){//             //     this.count = count + 1//             // }//             this.$store.dispatch('incrementIfOdd');//         },//         incrementAsync () {//           // setTimeout(() => {//             // const count = this.count//             // this.count = count + 1//           // }, 1000)//           this.$store.dispatch('incrementAsync')//         }//     }//     };// 优化 App.vue   代码格式
    export default{computed:{...mapState['count'],...mapGetters['eventOrOdd']},methods:{...mapActions(['increment','decrement','incrementIfOdd','incrementAsync'])}};</script><style type="stylus" rel="stylesheet/stylus"></style>

View Code

官方另一个案例购物车, store以目录结构呈现

https://github.com/vuejs/vuex/tree/dev/examples/shopping-cart

Vue--- VueX基础 (Vuex结构图数据流向)1.1相关推荐

  1. vuex基础-Vuex是什么呢?

    vuex Vuex是什么呢?它是Vue的状态管理模式,在使用vue的时候,需要在vue中各个组件之间传递值是很痛苦的,在vue中我们可以使用vuex来保存我们需要管理的状态值,值一旦被改变,所有引用该 ...

  2. vue-router路由、mixin混入、vue-resource、axios、计算属性watch、moment.js、vuex、vue-cli、数据双向绑定、搭建vue环境、vue实例、配置启动项

    路由vue-router介绍: // 1.前端路由核心:锚点值的改变,根据不同的锚点值,渲染指定dom位置的不同数据.// 2.vue中,模板数据不是通过ajax请求的,而是调用函数获取到模板内容// ...

  3. 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十三║Vue实战:Vuex 其实很简单

    前言 哈喽大家周五好,马上又是一个周末了,下周就是中秋了,下下周就是国庆啦,这里先祝福大家一个比一个假日嗨皮啦~~转眼我们的专题已经写了第 23 篇了,好几次都坚持不下去想要中断,不过每当看到群里的交 ...

  4. 视频教程-德国Vue.js2终极开发教程(含Vue路由和Vuex)-Vue

    德国Vue.js2终极开发教程(含Vue路由和Vuex) * Academind GmbH创始人,当前生活在德国慕尼黑 * 从15岁开始进入开发领域,前后端技术均很精通,毕业于慕尼黑大学硕士学位 * ...

  5. 德国Vue.js2终极开发教程(含Vue路由和Vuex)-Max-专题视频课程

    德国Vue.js2终极开发教程(含Vue路由和Vuex)-808人已学习 课程介绍         新手入门现代前端开发的不二选择 课程目标: * 学会从简单到复杂企业级应用的VueJS程序编写方法 ...

  6. vuex基础语法、state代码示例、mutations代码示例

    一.vuex基础语法 1.简介 Vuex 是一个专为 Vue.js 应用程序开发的状态(数据)管理模式. 简单来说就是方便管理管理组件之间的数据共享,原来我们那种父传子.子传父.兄弟组件传值的方式在小 ...

  7. vue中使用vuex(超详细)

    vuex是使用vue中必不可少的一部分,基于父子.兄弟组件,我们传值可能会很方便,但是如果是没有关联的组件之间要使用同一组数据,就显得很无能为力,那么vuex就很好的解决了我们这种问题,它相当于一个公 ...

  8. vue全家桶——vuex

    本文主要介绍vuex[状态管理模式],在介绍vuex前,需要重点说下一些概念 vue最大的特点之一.数据驱动视图,可以吧数据理解成状态,视图-view可以理解成用户看到的页面,页面是动态变化的,而这个 ...

  9. vue firebase_如何使用Vue.js,Vuex,Vuetify和Firebase构建SPA:使用Firebase添加身份验证...

    vue firebase 第4部分:了解如何使用Firebase添加身份验证和购物车 (Part 4: learn how to use Firebase to add authentication ...

最新文章

  1. SharedPreferences的使用
  2. python绘制直方图-python matplotlib库直方图绘制详解
  3. 【转】Spring 的下载、安装和使用
  4. 收藏|2021年阿里云开源镜像站最热门镜像王全梳理(附下载链接和Top20镜像王排名)
  5. 07.用户故事与敏捷方法——优秀用户故事准则笔记
  6. 目标检测——SSD的学习笔记
  7. aftershokz蓝牙搜不到_iphone xr黑屏无法开机怎么办?iphonexr蓝牙连不上怎么办?
  8. tensorflow之eval
  9. PAT (Advanced Level) Practice 1001 A+B Format (20分)
  10. js双击事件屏蔽单击事件
  11. 『GoLang』错误处理
  12. _corrupted_rollback_segments参数解决UNDO表空间损坏无法启动数据库的故障
  13. java imageio 内存问题_java imageio内存泄漏
  14. JSP中文乱码解决方案了解和TOMCAT中文乱码解决
  15. Mac全自动安装brew一键配置国内镜像源
  16. 搜狗拼音皮肤 php文件,搜狗拼音输入法皮肤制作
  17. 【从零建站3/n】错题集(已解决):服务器上再次进入有Echarts的页面,图表无法显示
  18. 漏斗周期漏斗数据关联优化
  19. WTD UI自动化和接口测试开发文档
  20. 打造最强终端之一:Fish shell简明教程

热门文章

  1. Python 基础 - 第三方模块PyYAML
  2. 台达PLC与三菱E740或D700变频器modbus 通讯案例
  3. Python读取excel文件内容并保存到SqlServer数据库
  4. 抹掉所有内容和设置 连接到icloud时出错 iphone还原出厂设置
  5. Win10查看网卡驱动的方法
  6. 双绞线的制作(常用568B)
  7. 云南大学c语言实验报告,云南大学软件学院C语言程序
  8. pubchem的官方API - PUG REST的使用教程(持续更新)
  9. Django项目创建
  10. 航顺 HK32F030C8T6 单片机MCU控制器