vuex使用案例(下)

5. state访问状态对象

使用computed计算

<template>
  <div class="hello">
      <h1>Hello Vuex</h1>
      <h5>{{count}}</h5>
      <p>
        <button @click="$store.commit('jia')">+</button>
        <button @click="$store.commit('jian')">-</button>
      </p>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default{
    name:'hello', //写上name的作用是,如果你页面报错了,他会提示你是那个页面报的错,很实用
    // 方法一
    // computed: {
    //   count(){
    //     return this.$store.state.count + 6
    //   }
    // }
    
    // 方法二 需要引入外部 mapState
    computed:mapState({
      count:state => state.count + 10
    })
</script>

6. mutations触发状态 (同步状态)

<template>
  <div class="hello">
      <h1>Hello Vuex</h1>
      <h5>{{count}}</h5>
      <p>
        <button @click="jia">+</button>
        <button @click="jian">-</button>
      </p>
  </div>
</template>
<script>
import {mapState,mapMutations} from 'vuex'
  export default{
    name:'hello', //写上name的作用是,如果你页面报错了,他会提示你是那个页面报的错,很实用
    //方法三
    computed: mapState([
      'count'
    ]),
    methods:{
      ...mapMutations([
          'jia',
          'jian'
      ])
    }
  }
</script>

7. getters计算属性

getter不能使用箭头函数,会改变this的指向
在store.js添加getters

// 计算
const getters = {
    count(state){
        return state.count + 66
    }
}

export default new Vuex.Store({
    state,
    mutations,
    getters
})
//count的参数就是上面定义的state对象
//getters中定义的方法名称和组件中使用的时候一定是一致的,定义的是count方法,使用的时候也用count,保持一致。
组件中使用

<script>
  import {mapState,mapMutations,mapGetters} from 'vuex'
  export default{
    name:'hello',
    computed: {
      ...mapState([
        'count'
      ]),
      ...mapGetters([
        'count'
      ])
    },
    methods:{
      ...mapMutations([
          'jia',
          'jian'
      ])
    }
  }
</script>

8. actions (异步状态)

在store.js添加actions

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

// 定义常量
const state = {
    count: 1
}

// mutations用来改变store状态  同步状态
const mutations = {
    jia(state){
        state.count ++
    },
    jian(state){
        state.count --
    },
}
// 计算属性
const getters = {
    count(state){
        return state.count + 66
    }
}
// 异步状态
const actions = {
    jiaplus(context){
        context.commit('jia') //调用mutations下面的方法
        setTimeout(()=>{
            context.commit('jian')
        },2000)
        alert('我先被执行了,然后两秒后调用jian的方法')
    },
    jianplus(context){
        context.commit('jian')
    }
}

export default new Vuex.Store({
    state,
    mutations,
    getters,
    actions
})
在组件中使用

<template>
  <div class="hello">
      <h1>Hello Vuex</h1>
      <h5>{{count}}</h5>
      <p>
        <button @click="jia">+</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:'hello',
    computed: {
      ...mapState([
        'count'
      ]),
      ...mapGetters([
        'count'
      ])
    },
    methods:{
      // 这里是数组的方式触发方法
      ...mapMutations([
          'jia',
          'jian'
      ]),
      // 换一中方式触发方法 用对象的方式
      ...mapActions({
        jiaplus: 'jiaplus',
        jianplus: 'jianplus'
      })
    }
  }
</script>

<style scoped>
    h5{
      font-size: 20px;
      color: red;
    }
</style>

9. modules 模块

适用于非常大的项目,且状态很多的情况下使用,便于管理

修改store.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const state = {
    count: 1
}
const mutations = {
    jia(state){
        state.count ++
    },
    jian(state){
        state.count --
    },
}
const getters = {
    count(state){
        return state.count + 66
    }
}
const actions = {
    jiaplus(context){
        context.commit('jia') //调用mutations下面的方法
        setTimeout(()=>{
            context.commit('jian')
        },2000)
        alert('我先被执行了,然后两秒后调用jian的方法')
    },
    jianplus(context){
        context.commit('jian')
    }
}

//module使用模块组的方式  moduleA
const moduleA = {
    state,
    mutations,
    getters,
    actions
}

// 模块B moduleB
const moduleB = {
    state: {
        count:108
    }
}

export default new Vuex.Store({
    modules: {
        a: moduleA,
        b: moduleB,
    }
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值