vue基础之状态管理vuex

Vuex实现状态管理

背景:Vue状态管理

如何引入VUEX

1.新建index.js文件,用来直接引用VUEX

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

//直接使用VUEX
Vue.use(Vuex)

//创建vuex实例
const store = new Vuex.store({
    
})

export default store

2.在main.js中引入store对象

import Vue from 'vue'
import App from 'App'
import router from './router'
import store from './store'

new Vuew({
    el:'#app',
    store,
    router,
    components:{
        App
    }
    templete:'<App/>'
    
    
})
  1. 状态声明和使用

     const store = new Vuex.store({
         state:{
             count:1
         }
     })
     
     
     <h1> {{this.$store.state.count}} </h1>
    
  2. getters

    const store = new Vuex.store({
        state:{
            count:1
        },
        getters:{
            getStateCount:function(state){
                return state.count + 1
            }
        }
    })
    
     <h1> {{this.$store.getStateCount}} </h1>
    
如何进行状态管理 (更改数据)
  1. 布局如下

     <p>{{this.$store.count}} </p>
     <button @click="add">ADD</button>
     <button @click="minus">MINUS</button>
    
  2. 布局对应的逻辑

     methods:{
         add:function(){
             this.$store.commit("add")
         },
         minus:function(){
             this.$store.commit("minus")
         }
     }
    
  3. vuex中store对象

     const store = new Vuex.store({
         state:{
             count:1
         },
         getters:{
             getStateCount:function(state){
                 return this.$store.state + 1
             }
         },
         mutations:{
               add:function(state){
                 state.count = state.count + 1
             },
             minus:function(){
                  state.count = state.count + 1
             }
         }
     })
    
如何优雅的进行状态管理
  1. 使用actions

     methods{
         add:function(){
             this.$store.dispatch("addF")
         },
         minus:function(){
         var n = 1;
              this.$store.dispatch("minusF" ,n)
         }
     }
    
    
     const store =  new Vuex.store({
         state:{
             count:1
         },
         getters:{
             getStateCount:function(state){
                 return state + 1
             }
         },
         mutations:{
             add:function(state){
                 state.count  = state.count  + 1
             },
             minus:function(state,n){
                 state.count = (state.count -1)*(n+1)
             }
         },
         actions:{
             addF:function(context){
                 context.commit("add")
             },
             minusF:function(context,n){
                 context.commit("minus",n)
             }
         }
     })
    

2.如果我们不喜欢这种在页面上使用“this. s t r o e . s t a t e . c o u n t ” 和 “ t h i s . stroe.state.count”和“this. stroe.state.countthis.store.dispatch(‘funName’)”这种很长的写法,那么我们可以使用mapState、mapGetters、mapActions就不会这么麻烦了;

<p>{{count1}} </p>

import {mapState,mapGetters,mapActions}from 'vuex'

couputed:{
    mapSate{
        count1:state=>store.count
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值