vuex demo

创建项目

创建项目
vue create vuex_demo
创建项目的时候选择自定义方式创建项目,选上 vuex(vue状态管理)模块

使用state

store

修改store/index.js,在state里保存共享数据count

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

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count:0
  },
  mutations: {
  },
  actions: {
  },
  modules: {
  }
})

Index.vue

把components目录下自动生成的 HelloWorld.vue删除,在components目录下创建Index.vue

<template>
  <div class="index">
    {{count}}
  </div>
</template>

<script>
  export default {
    name: "index",
    computed:{
      count(){
        return this.$store.state.count;
      }
    }
  }
</script>

App

修改App.vue

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <Index />
  </div>
</template>

<script>
import Index from './components/Index.vue'

export default {
  name: 'App',
  components: {
    Index
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

运行

cnpm run serve

84

使用mapState

修改store/index.js

添加一个state

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

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count:0,
    name:"xzw",
  },
  mutations: {
  },
  actions: {
  },
  modules: {
  }
})

修改components/Index.vue

使用mapState将count ,name分别映射出来

<template>
  <div class="index">
    {{count}}
    {{name}}
  </div>
</template>

<script>
import {mapState} from 'vuex'
  export default {
    name: "index",
    computed:{
      ...mapState(['count','name']),
    }
  }
</script>

运行

85

使用getter

修改store/index.js

在state中添加numbers,并且使用getters提供过滤方法

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

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count:0,
    name:"xzw",
    numbers:[0,1,2,3,4,5]
  },
  getters:{
    filterNumbers(state){
      return state.numbers.filter((item)=>{
        return item >3;
      })
    }
  },
  mutations: {
  },
  actions: {
  },
  modules: {
  }
})


修改components/Index.vue

<template>
  <div class="index">
    {{count}}
    {{name}}
    <br>
    {{filterNumbers.join()}}
  </div>
</template>

<script>
import {mapState} from 'vuex'
  export default {
    name: "index",
    computed:{
      ...mapState(['count','name']),
      filterNumbers(){
        return this.$store.getters.filterNumbers;
      }
    }
  }
</script>

运行

86

使用mutation

修改store/index.js

在mutations里面添加add方法

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

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count:0,
    name:"xzw",
    numbers:[0,1,2,3,4,5]
  },
  getters:{
    filterNumbers(state){
      return state.numbers.filter((item)=>{
        return item >3;
      })
    }
  },
  mutations: {
    add(state){
      state.count++;
    }
  },
  actions: {
  },
  modules: {
  }
})

修改components/Index.vue

添加add方法

<template>
  <div class="index">
    {{count}}
    {{name}}
    <br>
    {{filterNumbers.join()}}
    <br>
    <button @click="add">+</button>
  </div>
</template>

<script>
import {mapState} from 'vuex'
  export default {
    name: "index",
    computed:{
      ...mapState(['count','name']),
      filterNumbers(){
        return this.$store.getters.filterNumbers;
      }
    },
    methods:{
      add(){
        this.$store.commit('add');
        
      }
    }
  }
</script>

触发方法语句为:this.$store.commit(方法名);也可以使用辅助函数mapMutations代替:

methods:{
   ...mapMutations(['add']),
}

运行

点击加号七次则显示7

87

使用action

mutation有必须同步执行这个限制,我们在业务需求中有时候需要在获取ajax请求数据之后再操作或定时器操作数据,这些都属于异步请求,要用actions来实现。

88

修改store/index.js

在mutataions添加changeCount方法,在actions中触发

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

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count:0,
    name:"xzw",
    numbers:[0,1,2,3,4,5]
  },
  getters:{
    filterNumbers(state){
      return state.numbers.filter((item)=>{
        return item >3;
      })
    }
  },
  mutations: {
    add(state){
      state.count++;
    },
    changeCount(state){
      state.count=3000
    }
  },
  actions: {
    changeCount3s(context){
      setTimeout(()=>{
        context.commit('changeCount')
      },3000)
    }
  },
  modules: {
  }
})

修改components/Index.vue

使用dispatch触发actions

<template>
  <div class="index">
    {{count}}
    {{name}}
    <br>
    {{filterNumbers.join()}}
    <br>
    <button @click="add">+</button>
    <br>
    <button @click="changeCount">点击按钮3s改变值</button>
  </div>
</template>

<script>
import {mapState} from 'vuex'
  export default {
    name: "index",
    computed:{
      ...mapState(['count','name']),
      filterNumbers(){
        return this.$store.getters.filterNumbers;
      }
    },
    methods:{
      add(){
        this.$store.commit('add');
        
      },
      changeCount(){
        this.$store.dispatch('changeCount3s')
      }
    }
  }
</script>

运行

89

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×