vuex基本使用

一、什么是Vuex?

Vuex是一个专为vue.js应用程序开发的状态管理模式。

当我们构建一个中大型的单页面应用程序时,Vuex可以更好的帮助我们在组件外部统一管理状态。

二、Vuex的五个核心概念

State:基本数据,数据存放地。

Getters:从基本数据派生出来的数据

Mutations:提交更改数据的方法(同步),通过commit触发。

Actions:提交更改数据的方法(同步),通过dispatch触发,不能直接修改state,首先在组件中通过dispatch触发action,然后在action函数内部commit触发mutation,通过mutation修改state状态值。

Modules:模块化管理

三、基本使用

1、安装vuex

npm install vuex --save

2、在src目录下创建store文件夹,并在文件夹下新建index.js

 
// 导入vue及vuex
import Vue from 'vue'
import Vuex from 'vuex'

// 挂载vuex
Vue.use(Vuex)
 
// 创建vuex对象并向外暴露
export default new Vuex.Store({
 // 我们在Store里面定义一个state这么一个属性
 state: {
    count: 1 // 它里面有个count这样的字段,值是1
    data: 1,
  },
  // 同步方法, 调用方法,this.$store.commit("xxx")
  mutations: {
   increment (state) {
      // 变更状态
      state.count++
    },
    changeDataMut(state,params){  
      state.data = params;
    },
  },
  // 异步方法 调用方法,this.$store.dispatch("xxx") 
  actions: {
  	changeDataAct(context,params){  //触发mutations里的方法,action不能直接修改state里的数据
       let {commit} = context;
       commit('changeDataMut',params);
     }
  },
  //相当于computer计算属性
  getters: {
  },
  // 模块化注册
  modules: {
  }
})

3、挂载(main.js)

import store from './store/index.js'
Vue.prototype.$store = store

4、组件中使用

<template>
  <div>
  	{{this.$store.state.count}} 
  </div>
</template>
 
<script>
export default {
   
}
</script>
<style scoped>
</style>

5、修改state中的数据

<template>
  <div>
  
  	<div>{{this.$store.state.count}}</div>
  	<button @click='changeData1'>+</button>
  	
  	<div>{{this.$store.state.data}}</div>
  	<button @click='changeData2'>点击</button>
  	
  </div>
</template>
 
<script>
export default {
  data() {
    return {};
  },
  methods: {
    changeData1() {
      // 通过commit 触发 mutation 
      this.$store.commit("increment"); 
    },
    changeData2() {
        // 通过commit 触发 action 并传参
      this.$store.dispatch('changeDataAct',100);
    },},
};
</script>
<style scoped>
</style>```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3是目前最新的Vue.js版本,相比Vue2有很多新特性和更好的性能。而VuexVue.js官方推荐的状态管理工具,用于处理复杂的应用程序中的共享状态。 下面是Vue3和Vuex基本使用方法: 1. 在项目中安装Vue3和Vuex: ```bash npm install vue@next vuex@next --save ``` 2. 创建一个store.js文件用于配置Vuex: ```javascript import { createStore } from 'vuex' const store = createStore({ state: { count: 0 }, mutations: { increment(state) { state.count++ } }, actions: { increment(context) { context.commit('increment') } }, getters: { getCount(state) { return state.count } } }) export default store ``` 这里我们定义了一个state对象来存储应用程序的状态,一个mutation函数来改变状态,一个action函数来分发mutation,一个getter函数来获取状态。 3. 在main.js中引入store: ```javascript import { createApp } from 'vue' import App from './App.vue' import store from './store' createApp(App).use(store).mount('#app') ``` 4. 在组件使用Vuex: ```html <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> </div> </template> <script> import { mapGetters, mapActions } from 'vuex' export default { computed: { ...mapGetters(['getCount']), count() { return this.getCount } }, methods: { ...mapActions(['increment']) } } </script> ``` 这里我们使用Vuex提供的mapGetters和mapActions函数来映射state中的count属性和increment函数到组件中。 以上就是Vue3和Vuex基本使用方法,可以根据自己的需求进行扩展和修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值