vue中的vuex

第一步:安装 vuex

npm install vuex@3

安装完成后,在package.json文件中存在vuex版本:如图

在main.js文件中导入store的配置文件

import Vue from 'vue'
import App from './App.vue'
import store from "@/store/store";//导入store的配置文件

Vue.config.productionTip = false

new Vue({
  store,//在vue实例中引用
  render: function (h) { return h(App) },
}).$mount('#app')

在src文件夹下创建文件夹store。store下再创建store.js文件,在store.js文件中:

store分为:

state:{}该对象中声明的值在其他所有组件中都可直接调用

getters:{}监听state中的属性值是否变动,当属性值发生变动时getters会主动监测并进行改变,不需要进行显示调动;

mutations:{}同步修改store中的值

actions:{}异步的方式触发mutations,间接的实现对state的修改

modules:{}让每一个模块拥有自己的state、mutation、action、getters

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

//全局注册vuex
Vue.use(Vuex)

//定义全局的store
export default new Vuex.Store({
    state:{//Vuex核心
        //定义状态属性并进行初始化,在组件中可以通过 this.$store.state.属性名 直接获取属性值
        count:0,
        age:0
    },

    //getters:
    //(1)类似于组件的computed计算属性;
    //(2)作用:监听state中属性的变化(获取state中的值);
    //(3)用法:this.$store.getters.属性名
    getters:{
        getAge(state){//this.$store.getters.getAge可以获取到state中的age
            return state.age
        }//getAge值改变时getters会主动监测并进行改变,不需要进行显示调动
    },

    //(1)mutations的作用:用于修改state中的值
    //(2)要求:只能是同步的
    //(3)定义方法时需要注意的问题:
        //a、方法中的第一个参数默认为state
        //b、方法的第二个参数:表示调用该方法时传入的参数,本质是一个对象
    mutations:{
        addCount(state,obj){//让state的count加一个指定的值
            return state.count += obj.num
        },
        subCount(state,obj){//让state的count减一个指定的值
            return state.count -= obj.num
        }
    },
    //(1)actions的作用:异步的方式触发mutations,间接的实现对state的修改
    //(2)定义方法:在异步方法中带有一个参数context,该参数的作用是调用commit触发mutations
    actions:{
        addCountAsyn(context){//异步操作:2秒后让count加1
            setTimeout(()=>{
                //触发mutations中的方法
                context.commit('addCount',{num:1})
            },2000)
        },
        subCountAsyn(context){//异步操作:2秒后让count加1
            setTimeout(()=>{
                //触发mutations中的方法
                context.commit('subCount',{num:1})
            },2000)
        }
    },

    modules:{

    }

})

创建组件:

<template>
  <div>
    <h2>通过getters来获取vuex(store)中age属性: {{ getAge }}</h2>
    <p>Count:  {{ count }}</p>
    <button @click="handlerAdd()">同步加1</button>
    <button @click="handlerSub()">同步减1</button>
    <button @click="handlerAddAsyn()">异步加1</button>
    <button @click="handlerSubAsyn()">异步减1</button>
  </div>
</template>

<script>
export default {
  name: "Home",

  computed:{
    count(){
      return this.$store.state.count  //直接获取state的count属性值
    },
    getAge(){
      return this.$store.getters.getAge //是通过getters的getAge方法来获取state的age属性值
    }
  },

  methods:{
    handlerAdd(){//同步加1:触发mutations中的addCount方法
        this.$store.commit('addCount',{num:1})
    },
    handlerSub(){//同步减1:触发mutations中的subCount方法
      this.$store.commit('subCount',{num:1})
    },
    handlerAddAsyn(){//异步加1:触发actions中的addCountAsyn方法
      this.$store.dispatch('addCountAsyn')
    },
    handlerSubAsyn(){//异步减1:触发actions中的addCountAsyn方法
      this.$store.dispatch('subCountAsyn')
    }
  }
}
</script>

<style scoped>

</style>

在主页中调用组件:

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <Home />
  </div>
</template>

<script>

import Home from "@/components/Home";
export default {
  name: 'App',
  components: {
    Home,
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue使用Vuex,你需要按照以下步骤进行设置: 1. 安装Vuex:在项目根目录下执行以下命令安装Vuex依赖: ``` npm install vuex ``` 2. 创建store:在项目的src目录下创建一个名为store的文件夹,然后在该文件夹创建一个名为index.js的文件。index.js文件是Vuex的核心文件,用于创建和配置store。 3. 在index.js导入VueVuex,并使用Vue.use(Vuex)注册Vuex插件。然后创建一个新的Vuex store实例: ```javascript import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ // 在这里定义你的state、mutations、actions等 }) export default store ``` 4. 在main.js导入store并将其作为Vue实例的一个选项: ```javascript import Vue from 'vue' import App from './App.vue' import store from './store' new Vue({ store, render: h => h(App) }).$mount('#app') ``` 5. 在store文件夹创建state、mutations和actions等模块,以及需要存储的数据和对数据进行操作的方法。例如,你可以在index.js定义一个名为counter的模块: ```javascript const counter = { state: { count: 0 }, mutations: { increment(state) { state.count++ }, decrement(state) { state.count-- } }, actions: { increment(context) { context.commit('increment') }, decrement(context) { context.commit('decrement') } } } export default new Vuex.Store({ modules: { counter } }) ``` 6. 现在你可以在Vue组件使用Vuex了。你可以通过计算属性computed和方法methods来访问store的状态或触发mutations和actions。例如,你可以在组件使用以下代码: ```javascript <template> <div> <p>{{ count }}</p> <button @click="increment">Increment</button> <button @click="decrement">Decrement</button> </div> </template> <script> export default { computed: { count() { return this.$store.state.counter.count } }, methods: { increment() { this.$store.dispatch('counter/increment') }, decrement() { this.$store.dispatch('counter/decrement') } } } </script> ``` 这样就完成了在Vue使用Vuex的设置。你可以根据自己的需求来定义和使用Vuex的模块、状态、mutations和actions等。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值