Vuex组件通讯

Vuex(组件之间通讯) 是一个专为 Vue.js 应用程序开发的状态管理模式。一般使用与大型项目。

  • vuex是采用集中式管理组件依赖的共享数据的一个工具,可以解决不同组件数据共享问题。

总结:

  1. state: 用来定义数据

  2. 修改state状态必须通过mutations

  3. mutations只能执行同步代码,类似ajax,定时器之类的代码不能在mutations中执行

  4. 执行异步代码,要通过actions,然后将数据提交给mutations才可以完成

  5. getters从 store 中的 state 中派生出一些状态

  6. modules: 进入项目学习

vuex基础-初始化

安装:yarn add vuex@3.6.2

  •  在main.js中 import Vuex from 'vuex'

  • 在main.js中 Vue.use(Vuex) => 调用了 vuex中的 一个install方法

  • const store = new Vuex.Store({...配置项})

  • 在根实例配置 store 选项指向 store 实例对象

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(vuex)
const store = new Vuex.Store({})
new Vue({
  el: '#app',
  store
})

vuex基础-state

state是放置所有公共状态的属性,如果你有一个公共状态数据 , 你只需要定义在 state对象中

定义state

const store = new Vuex.Store({
  state: {
    uname: 'ha',
    count: 0,
    word: '',
  },

原始形式- 插值表达式

组件中可以使用 this.$store 获取到vuex中的store对象实例,可通过state属性属性获取count, 如下

<div> state的数据:{{ $store.state.count }}</div>

计算属性 - 将state属性定义在计算属性中

  computed: {
    count () {
      return this.$store.state.count
    }
  }

辅助函数 - mapState

mapState是辅助函数,帮助我们把store中的数据映射到 组件的计算属性中, 它属于一种方便用法

属于谁的方法: vuex

是什么: 是一个函数

作用: 将vuex中state的数据映射为了计算属性

怎么用: 调用,传递数组,数组每一项,就是state中的数据名,写哪个数据,就会将哪个数据处理为计算属性

返回值: 对象, 存的就是处理好的计算属性

第一步:导入mapState

import { mapState } from 'vuex'

第二步:采用数组形式引入state属性

mapState(['count']) 

count () {
    return this.$store.state.count
}

第三步:利用延展运算符将导出的状态映射给计算属性

  computed: {
    ...mapState(['count'])
  }

 <div> state的数据:{{ count }}</div>

vuex基础-mutations

state数据的修改只能通过mutations,并且mutations必须是同步更新,目的是形成数据快照

数据快照:一次mutation的执行,立刻得到一种视图状态,因为是立刻,所以必须是同步

定义mutations

mutations是一个对象,对象中存放修改state的方法

mutations: {
    // 方法里参数 第一个参数是当前store的state属性
    // payload 载荷 运输参数 调用mutaiions的时候 可以传递参数 传递载荷
    addCount (state,payload) {
      state.count += 1
    }
  },

<template>
  <button @click="addCount">+1</button>
</template>

<script>
export default {
    methods: {
    //   调用方法
      addCount () {
         // 调用store中的mutations 提交给muations
        // commit('muations名称', 2)
        this.$store.commit('addCount', 10)  // 直接调用mutations
    }
  }
}
</script>

带参数的传递

    addCount (state, payload) {
        state.count += payload
    }
    this.$store.commit('addCount', 10)

辅助函数 - mapMutations

mapMutations和mapState很像,它把位于mutations中的方法提取了出来,我们可以将它导入

import  { mapMutations } from 'vuex'
methods: {
    ...mapMutations(['addCount'])
}

注意: Vuex中mutations中要求不能写异步代码,如果有异步的ajax请求,应该放置在actions中

vuex基础-actions

state是存放数据的,mutations是同步更新数据,actions则负责进行异步操作

定义actions
 

 actions: {
  //  获取异步的数据 context表示当前的store的实例 可以通过 context.state 获取状态 也可以通过context.commit 来提交mutations, 也可以 context.diapatch调用其他的action
    getAsyncCount (context) {
      setTimeout(function(){
        // 一秒钟之后 要给一个数 去修改state
        context.commit('addCount', 123)
      }, 1000)
    }
 } 

原始调用 - $store

 addAsyncCount () {
     this.$store.dispatch('getAsyncCount')
 }

 传参调用

 addAsyncCount () {
     this.$store.dispatch('getAsyncCount', 123)
 }

 辅助函数 -mapActions

import { mapActions } from 'vuex'
methods: {
    ...mapActions(['getAsyncCount'])
}

直接通过 this.方法就可以调用

<button @click="getAsyncCount(111)">+异步</button>

vuex基础-getters

除了state之外,有时我们还需要从state中派生出一些状态,这些状态是依赖state的,此时会用到getters

定义getters

  getters: {
    // getters函数的第一个参数是 state
    // 必须要有返回值
     filterList:  state =>  state.list.filter(item => item > 5)
  }

<div>{{ $store.getters.filterList }}</div>

辅助函数 - mapGetters

computed: {
    ...mapGetters(['filterList'])
}

 <div>{{ filterList }}</div>

综合案例:

<template>
  <div>
    <!-- {{ $store.state.uname }}
    {{ $store.state.count }} -->
    <h1>{{ count }}</h1>
    <br />
    <h2>{{ uname }}</h2>
    <br />
    <button @click="addCount">add</button>
    <button @click="delCount">sub</button>
    <button @click="asyncadd">异步增加</button>
    <h1>getters里的平方{{ $store.getters.countSqu }}</h1>
    <h5>mapGetters平方:{{ countSqu }}</h5>
    <Me></Me>
  </div>
</template>

<script>
import { mapState, mapActions, mapGetters } from 'vuex'
import Me from './components/Me.vue'
export default {
  components: { Me },
  computed: {
    // count() {
    //   return this.$store.state.count
    // },
    ...mapState(['uname', 'count']),
    ...mapGetters(['countSqu']),
  },
  methods: {
    ...mapActions(['asyncAddCount']),
    //同步操作用$store.commitcommit
    addCount() {
      this.$store.commit('addCount', 10)
    },
    delCount() {
      this.$store.commit('delCount', 5)
    },
    //触发异步操作: 用$store.dispatch  (定时器 ajax处理)
    asyncadd() {
      // this.$store.dispatch('asyncAddCount', 6) //每次加6 ,
      this.asyncAddCount(4) //每次加444 用mapActions 调用
    },
  },
  created() {
    console.log(this)
  },
}
</script>

<style></style>
import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'
Vue.config.productionTip = false
Vue.use(Vuex) //让vue使用Vuex
//创建一个实例
const store = new Vuex.Store({
  state: {
    uname: 'ha',
    count: 0,
    word: '',
  },
  getters: {
    // countSqu(state) {
    //   console.log(state)
    //   return state.count ** 2
    // },
    countSqu: (state) => state.count ** 2,
  },
  //state指的式state的里面的count参数,payload值是传入的每次加的值
  // mutations: 所有共享的数据通过mutations运行工作
  mutations: {
    //同步
    addCount(state, payload = 1) {
      state.count += payload
    },
    delCount(state, payload = 1) {
      state.count -= payload
    },
  },
  actions: {
    //异步 通过actions调用mutation里面的addcount,然后改变count里面的数据
    //payload接收传来的参数
    asyncAddCount(context, payload) {
      console.log('context', context)
      setTimeout(() => {
        context.commit('addCount', payload)
      }, 1000)
    },
  },
})

new Vue({
  store, //让Vue的实例使用创建的store实例

  render: (h) => h(App),
}).$mount('#app')

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值