vue的状态管理vuex

入门

一、首先,初始化vue工程

vue init webpack vue01

最后一项可以选择no,然后使用cnpm进行安装,防止卡死。

二、安装vuex

cnpm install --save vuex

三、在src目录下创建store目录,并创建index.js文件
在这里插入图片描述

//定义vuex的store对象


//1.显示调用vuex
import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex)

//2.创建状态管理对象
let store = new Vuex.Store({
    state:{
        //定义共享变量
        count:0
    }
})

//3.导出对象
export default store

四、在main.js中启用store

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'

Vue.config.productionTip = false

//需要配置状态管理

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  components: { App },
  template: '<App/>'
})

五、页面中显示共享数据

$store内置对象(容器)
state(状态管理对象)

{{$store.state.count}}

mutation改变状态值

在store容器中,定义mutation,其中定义改变状态值的方法。

//2.创建状态管理对象
let store = new Vuex.Store({
    state:{
        //定义共享变量
        count:0
    },
    mutations:{
        increament:function(state,step){
            state.count+=step
        }
    }
})

页面中调用改变状态值的方法

this.$store.commit("方法名",参数值)
methods:{
    test:function(){
      //不能直接改变共享变量,不能直接调用multation的方法
      this.$store.commit("increament",2)
    }
  }

也可以使用actions,来间接调用mutations

//2.创建状态管理对象
let store = new Vuex.Store({
    state:{
        //定义共享变量
        count:0
    },
    mutations:{
        increament:function(state,step){
            state.count+=step
        }
    },
    //间接调用mutations
    actions:{
        increamentAction:function(context){
            context.commit("increament",1)
        }
    }
})
methods:{
    test:function(){
      //不能直接改变共享变量,不能直接调用multation的方法
      //this.$store.commit("increament",2)
      this.$store.dispatch("increamentAction")
    }
  }

派生属性getters

定义派生属性level,让它随着count的值而变化

//2.创建状态管理对象
let store = new Vuex.Store({
    state:{
        //定义共享变量
        count:0
    },
    mutations:{
        increament:function(state,step){
            state.count+=step
        }
    },
    //间接调用mutations
    actions:{
        increamentAction:function(context){
            context.commit("increament",1)
        }
    },
    //派生属性 getters
    getters:{
        level:function(state){
            if(state.count < 5){
                return "低级"
            } else if (state.count >= 5){
                return "中级"
            }
        }
    }

})
 级别:{{$store.getters.level}}

在这里插入图片描述

模块化

基本目录结构:

  • 共享属性写在modules目录的不同js文件中
  • 派生属性写在getters.js中
  • 拼接的任务交给index.js

在这里插入图片描述
一、创建目录
在这里插入图片描述
二、创建共享属性module1

export default{
    state:{
        //定义共享变量
        count:0
    },
    mutations:{
        increament:function(state,step){
            state.count+=step
        }
    },
    //间接调用mutations
    actions:{
        increamentAction:function(context){
            context.commit("increament",1)
        }
    },
}

三、创建派生属性level

export default{
     //派生属性 getters
    level:function(state){
        if(state.a.count < 5){
            return "低级"
        } else if (state.a.count >= 5){
            return "中级"
        }
    }
}

四、拼接共享属性和派生属性

//1.显示调用vuex
import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex)
import module1 from "./modules/module1"
import getters from "./getters"

let store = new Vuex.Store({
    getters,
    modules:{
        a:module1
    }
})

export default store
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值