Vue Vuex状态管理

关于VueX

VueX是适用于在Vue项目开发时使用的状态管理工具。Vue为这些被多个组件频繁使用的值提供了一个统一管理的工具——VueX。在具有VueX的Vue项目中,我们只需要把这些值定义在VueX中,即可在整个Vue项目的组件中使用。

1. 安装

npm i vuex -s

随后会出来一个store目录 ,在下面新建一个index.js

2. 使用

import Vue  from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
    //存放属性就是所要管理的状态
    state:{
        user:{
            userName:'',
            passWord:''
        },
        loginSate:false
    }
})    

3. 把store挂载到当前项目的Vue实例中去 main.js

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

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

4. 使用 

4.1 store/index.js state里面的自定义的属性 

<template>
  <div class="mainContainer">
    <h2>欢迎登陆{{$store.state.user.userName}}</h2>
  </div>
</template>
<script>
export default {
  data() {
    return {
      
    };
  },
};
</script>

4.2 store/index.js 定义 mutations

import Vue  from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
    state:{
        loginSate:false
    },
    mutations:{
     //定义方法修改state定义的属性
        checkLogin(state){
            state.loginSate = true
        }
    }
})

4.3 使用store/index.js  下面的mutations下面定义的方法this.$store.commit('定义的方法名称',要传的参数)

<template>
  <div class="form-container">
    <div class="logo">登录</div>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data(){
    return{
      isLogin:true
    }
  },
  mouted:{
     this.checkLogin();
  },
  methods: {
    // 登陆过直接去首页页面
    checkLogin() {
      this.$store.commit("checkLogin", this.isLogin);
    }
  }
};
</script>

   

4.4  store/index.js 定义 action

import Vue  from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
    state:{
        user:{
            userName:'',
            passWord:''
        },
        loginSate:false
    },
    getters:{
        getUserName(state,userName){
            state.userName=userName 
        }
    },
    mutations:{
        changeUserInfo(state,data){
            state.user.userName=data.userName
            state.user.passWord=data.passWord
            state.loginSate=data.loginState
            
        },
        checkLogin(state){
            state.loginSate = true
        }
    },
    actions:{
        changes({ commit }, data){
            commit('changeUserInfo', data);
        }
    }
})

使用

<template>
  <div class="form-container">
    <div class="logo">登录</div>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data(){
    return{
    user:{
            userName:'',
            passWord:'',
       loginState:false
        },
    }
  },
  mouted:{
     this.checkLogin();
  },
  methods: {
    // 登陆过直接去首页页面
    checkLogin() {
      this.$store.dispatch("changes", this.user);
    }
  }
};
</script>

store的 模块化  modules

1.模块化思想

 首先当项目里面需要使用store的地方增多的时候,所有的变量都写在同一个index.jsstate里面可读性会很差。

2. 如何使用store里面modules

 2.1. 首先创建一个modules文件夹,在文件夹内创建需要分模块的js文件

 2.2. js文件内的文件需要使用const user={ state:{},actions:{},mutations:{} }  export.default 对象名

2.3. index.js文件引入 import  声明的名称 from  ' js文件地址 '

2.4. 使用就和index.js声明的一样使用 this.$store.state.文件.变量名称  this.$store.commit('方法名',要传入的值)


                
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值