vue之vuex

简介: vue之vuex

1.安装vuex模块

npm i vuex --save-dev

 

//main.js文件
import Vue from 'vue'
import store from './store'
import vuex from './vuex.vue'
 
new Vue({
  el:'#app',
  store,
  render:xx=>xx(vuex)
})
 
// store.js文件
import Vue from 'vue'
import Vuex from 'vuex'
//import Parent from './tranistion.vue'
Vue.use(Vuex) 
 
const state = {
  count:44
}
const mutations = {
  jia(state){
    state.count ++
  },
  jian(state){
    state.count --
  }
}
export default new Vuex.Store({
  state,
  mutations
})
 
 
// vuex.vue 文件
<template>
  <div id="app">
    <h1>Hello Vuex</h1>
    <p>{{$store.state.count}}</p>
    <p>
      <button @click="$store.commit('jia')">+</button>
      <button @click="$store.commit('jian')">-</button>
    </p>
  </div>
</template>

 运行 npm run dev

2. state访问状态对象

   

// 修改vuex.vue
<template>
  <div id="app">
    <h1>Hello Vuex</h1>
    <p>{{$store.state.count}}  count->{{count}}</p>
    <p>
      <button @click="$store.commit('jia')">+</button>
      <button @click="$store.commit('jian')">-</button>
    </p>
  </div>
</template>
 
<script>
  import { mapState } from 'vuex'
  export default{
    name:'app',
    //computed:{
    //  count(){
    //      return this.$store.state.count+1
    //  }
    //}
    computed:mapState([
      "count"
    ])
  }
</script>

3. Mutations触发状态

       

<template>
  <div id="app">
    <h1>Hello Vuex</h1>
    <p>{{$store.state.count}}  count->{{count}}</p>
    <p>
      <button @click="$store.commit('jia',{a:10})">+</button>
      <button @click="jian">-</button>
    </p>
  </div>
</template>
 
<script>
  import { mapState,mapMutations } from 'vuex'
  export default{
    name:'app',
    //computed:{
    //  count(){
    //      return this.$store.state.count+1
    //  }
    //}
    computed:mapState([
      "count"
    ]),
    methods:mapMutations([
       'jia',
       'jian'
    ])
  }
</script>

4.getters

//store.js
import Vue from 'vue'
import Vuex from 'vuex'
//import Parent from './tranistion.vue'
Vue.use(Vuex) 
 
const state = {
  count:44
}
const mutations = {
  jia(state,obj){
    console.log(obj.a);
    state.count = state.count + obj.a
  },
  jian(state){
    state.count --
  }
}
const getters = {
  count:function(state){
    return state.count += 99
  }
}
export default new Vuex.Store({
  state,
  mutations,
  getters
})
 
// vuex.vue
<template>
  <div id="app">
    <h1>Hello Vuex</h1>
    <p>{{$store.state.count}}  count->{{count}}</p>
    <p>
      <button @click="$store.commit('jia',{a:10})">+</button>
      <button @click="jian">-</button>
    </p>
  </div>
</template>
 
<script>
  import { mapState,mapMutations,mapGetters } from 'vuex'
  export default{
    name:'app',
    computed:{
      ...mapState([
        "count"
      ]),
      count(){
        return this.$store.getters
      }
    },
    methods:mapMutations([
       'jia',
       'jian'
    ])
  }
</script>

5.actions

//vuex.vue
<template>
  <div id="app">
    <h1>Hello Vuex</h1>
    <p>{{$store.state.count}}  count->{{count}}</p>
    <p>
      <button @click="$store.commit('jia',{a:10})">+</button>
      <button @click="jian">-</button>
    </p>
    <p>
      <button @click="jiaplus">+plus</button>
      <button @click="jianplus">-plus</button>
    </p>
  </div>
</template>
 
<script>
  import { mapState,mapMutations,mapGetters,mapActions } from 'vuex'
  export default{
    name:'app',
    computed:{
      ...mapState([
        "count"
      ]),
      //count(){
      //  return this.$store.getters
      //}
    },
    methods:{
       ...mapMutations([
           'jia',
          'jian'
       ]),
       ...mapActions([
         'jiaplus'
       ]),
       ...mapActions({
           jianplus:'jianplus'
       })
    }
    }
</script>
 
// store.js
import Vue from 'vue'
import Vuex from 'vuex'
//import Parent from './tranistion.vue'
Vue.use(Vuex) 
 
const state = {
  count:44
}
const mutations = {
  jia(state,obj){
    console.log(obj.a);
    state.count = state.count + obj.a
  },
  jian(state){
    state.count --
  }
}
const getters = {
  count:function(state){
    return state.count += 99
  }
}
const actions = {
  jiaplus (context){
    context.commit('jia',{a:1});
    setTimeout(()=>{
      context.commit('jian')
    },3000)
    console.log('我先被执行了')
  },
  jianplus({commit}){
    commit('jian')
  }
}
export default new Vuex.Store({
  state,
  mutations,
  getters,
  actions
})
 

6.module

// store.js
import Vue from 'vue'
import Vuex from 'vuex'
//import Parent from './tranistion.vue'
Vue.use(Vuex) 
 
const state = {
  count:44
}
const mutations = {
  jia(state,obj){
    console.log(obj.a);
    state.count = state.count + obj.a
  },
  jian(state){
    state.count --
  }
}
const getters = {
  count:function(state){
    return state.count += 99
  }
}
const actions = {
  jiaplus (context){
    context.commit('jia',{a:1});
    setTimeout(()=>{
      context.commit('jian')
    },3000)
    console.log('我先被执行了')
  },
  jianplus({commit}){
    commit('jian')
  }
}
const moduleA = {
  state,
  mutations,
  getters,
  actions
}
const moduleB = {
  state:{count:99}
}
export default new Vuex.Store({
  modules:{
    a:moduleA,
    b:moduleB
  }
})
 
// vuex.vue
<template>
  <div id="app">
    <h1>Hello Vuex</h1>
    <p>{{$store.state.a.count}} --- {{count}}</p>
    <!--p>{{$store.state.count}}  count->{{count}}</p-->
    <p>
      <button @click="$store.commit('jia',{a:10})">+</button>
      <button @click="jian">-</button>
    </p>
    <p>
      <button @click="jiaplus">+plus</button>
      <button @click="jianplus">-plus</button>
    </p>
  </div>
</template>
 
<script>
  import { mapState,mapMutations,mapGetters,mapActions } from 'vuex'
  export default{
    name:'app',
    computed:{
      //...mapState([
      //  "count"
      //]),
      count(){
        return this.$store.state.a.count
      }
    },
    methods:{
       ...mapMutations([
           'jia',
          'jian'
       ]),
       ...mapActions([
         'jiaplus'
       ]),
       ...mapActions({
           jianplus:'jianplus'
       })
    }
    }
</script>


目录
相关文章
|
6天前
|
JavaScript
【vue】如何跳转路由到指定页面位置
【vue】如何跳转路由到指定页面位置
9 0
|
6天前
|
JavaScript 前端开发
【vue】iview如何把input输入框和点击输入框之后的边框去掉
【vue】iview如何把input输入框和点击输入框之后的边框去掉
12 0
|
2天前
|
资源调度 JavaScript 前端开发
【vue】vue中的路由vue-router,vue-cli脚手架详细使用教程
【vue】vue中的路由vue-router,vue-cli脚手架详细使用教程
|
2天前
|
JavaScript 前端开发
vue组件化开发流程梳理,拿来即用
vue组件化开发流程梳理,拿来即用
|
2天前
|
JavaScript
Vue 中如何模块化使用 Vuex
Vue 中如何模块化使用 Vuex
|
2天前
|
JavaScript Go
Vue路由跳转及路由传参
Vue路由跳转及路由传参
|
3天前
|
JavaScript 前端开发 BI
采用前后端分离Vue,Ant-Design技术开发的(手麻系统成品源码)适用于三甲医院
开发环境 技术架构:前后端分离 开发语言:C#.net6.0 开发工具:vs2022,vscode 前端框架:Vue,Ant-Design 后端框架:百小僧开源框架 数 据 库:sqlserver2019
采用前后端分离Vue,Ant-Design技术开发的(手麻系统成品源码)适用于三甲医院
|
5天前
|
监控 JavaScript
Vue中的数据变化监控与响应——深入理解Watchers
Vue中的数据变化监控与响应——深入理解Watchers
|
5天前
|
JavaScript 安全 前端开发
Vue 项目中的权限管理:让页面也学会说“你无权访问!
Vue 项目中的权限管理:让页面也学会说“你无权访问!
15 3
|
5天前
|
JavaScript 测试技术 开发者
Vue 3 Vuex:构建更强大的状态管理系统
Vue 3 Vuex:构建更强大的状态管理系统
13 1