首页 > 其他分享 >vue vuex 学习小demo

vue vuex 学习小demo

时间:2023-01-03 15:02:09浏览次数:37  
标签:count ... vue demo state vuex store


vue vuex 学习小demo_Vue

创建store.js 

引入并使用vuex

import Vuex from 'vuex'

Vue.use(Vuex)

1.使用new Vuex.Store创建({})创建store

2.创建state:{}存放数据

3.mutations:{}存放方法

4.actions:{}用于调用异步方法

5.getters:{}用于数据计算

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
//仓库数据
state:{
count:0,
name:"Winter Wang"
},
//方法 接收参数为state中的数据
mutations:{
add(state){
state.count++
},
sub(state){
state.count--
},
addN(state,step){
state.count+=step
},
subN(state,step){
state.count-=step
}
},
//如果actions里面想要修改store里面的数据,只能通过commit去触发mutations里面的方法
//不能直接修改
//context.commit('add')触发mutations里面的方法变成异步
actions:{
addAsync(context){
setTimeout(()=>{
context.commit('add')
},2000)
},
subAsync(context,step){
setTimeout(()=>{
context.commit('subN',step)
},2000)
},
},
//用于对store里面的数据进行加工处理形成新的数据
//store中的数据变化之后,getter包装中的数据也会跟着变化
//getters不会修改store里面的数据
getters:{
showNum:state=>{
return `${state.name}调用的是${state.count}`
}
}
})

Add.vue 上半部分加的组件

//vuex中不允许组件直接修改store中的数据逻辑
//只能展示,逻辑在vuex中处理
//方便集中管理状态
<template>
<div>
<h3>当前最新count的值为:{{count}}</h3>
<h2>{{showNum}}</h2>
<button @click="handleAdd">+1</button>
<button @click="handleAddN">+10</button>
<button @click="addAsync">延时+1</button>
</div>
</template>

<script>
import {mapState,mapMutations,mapActions,mapGetters} from 'vuex'
export default {
data(){
return{

}
},
methods:{
handleAdd(){
this.add()
},
handleAddN(){
//commit用来调用某个mutations里面的函数
this.$store.commit('addN',10)
},

//调用Mutations的第二种方法
...mapMutations(['add',"addN"]),

//调用Actions里面的方法,方法如果是...映射过来,是可以直接用的,不需要再添加一个事件去接收
...mapActions(['addAsync'])
},
//使用计算属性调取state中的数据
computed:{
...mapState(["count"]),
...mapGetters(['showNum'])
}
}
</script>

Sub.vue组件

<template>
<div>
<h3>当前最新count的值为:{{count}}</h3>
<button @click="handleSub">-1</button>
<button @click="handleSubN">延时-10</button>
</div>
</template>

<script>
//引入vuex要用的内容
import {mapState,mapMutations,mapActions} from 'vuex'
export default {
data(){
return{

}
},
methods:{
//调用Mutations要用的方法
...mapMutations(['sub']),
//在自定义的click事件里,调用this.sub()即可
handleSub(){
this.sub()
},

//调用Actions里面的异步方法
...mapActions(['subAsync']),
handleSubN(){
this.subAsync(10)
}
},
computed:{
...mapState(["count"])
}
}
</script>

父组件 index.vue

<template>
<div>
<Add></Add>
<p>--------------------</p>
<Sub></Sub>
</div>
</template>

<script>
import Add from './Add.vue'
import Sub from './Sub.vue'
export default {
components:{
Add,Sub
},
data(){
return{

}
}
}
</script>

 

标签:count,...,vue,demo,state,vuex,store
From: https://blog.51cto.com/u_12422954/5985784

相关文章

  • vue3.0的全局api变化
    1.全局api使用的变化:vue3已经去除Vue语法,取代的是用createApp创建的app  2.其他改变2.1data函数的变化,在vue3data必须是一个函数,否则报错     2.2过......
  • vue3.0新组件
    1.fragment1.1解释和意义   1.2使用:没有特定的标签,直接不使用根标签即可2.teleport(传送)1.1解释和意义:不管嵌套多少层都可以直接进行组件传送1.2使用:t......
  • ngnix conf配置 vue router
    #usernobody;worker_processes1;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx......
  • vue2 vant的定制主题26
    main.js//导入安装vant组件库importVantfrom'vant';//为了覆盖默认的less变量,需要把css后缀名改为lessimport'vant/lib/index.less';Vue.use(Vant);vue......
  • vue实现扫描二维码(@zxing/library和vue-qrcode-reader)
    ###环境vue2 "@zxing/library": "^0.19.1", "vue-qrcode-reader": "^3.1.0",扫码方式有三种,第一微信jssdk自带的扫一扫功能,优点:无兼容性问题,只要微信能扫一扫......
  • vue-qrcode-reader实现简单的实时扫一扫功能
    1.下载与安装插件vue-qecode-reader官网:https://gruhn.github.io/vue-qrcode-reader/demos/Simple.htmlnpminstall--savevue-qecode-reader或者cnpminstall-......
  • antd vue3 图片 手动上传
    <template><a-uploadv-model:file-list="fileList"name="avatar"list-type="picture-card"class="avatar-uploader":show-upload-list="false......
  • vue3和vue2对比
    compostionapi:组合api/注入api(3合成型apiCompositionAPI|2选项型apiOptionsAPI)双向数据绑定、响应式原理api的改变this在vue3中与vue2中代表着完成不一样的东西......
  • vue学习
    vue3中ref函数和reactive函数1、ref函数作用:定义一个响应式的数据语法:constxx=ref(initValue)创建一个包含响应式数据的引用对象(reference对象,简称ref对象)js中......
  • Vue拖动指令
    drag.jsimport_from"lodash";classDrap{constructor(el,option={parent:null}){this.el=el;this.x=0;this.y=0;this.el.sty......