vuex

Vuex是什么?

Vuex是一个专门为Vue.js应用程序开发的状态管理插件,它采用集中式存储管理应用的所有组件的状态,而改变状态的唯一方法是提交mutation。

什么时候使用Vuex?

  • 多个组件依赖于同一状态时
  • 来自不同组件的行为需要变更同一状态

怎样引用Vuex

  • 先安装依赖 npm i vuex -- save

  • 在项目目录src中建立store文件夹

  • 在store文件夹下新建index.js文件,写入:

import Vue from 'Vue'
import Vuex from 'Vuex'
Vue.use(Vuex)
// 创建vuex实例对象
const store = new Vuex.store({
  state: {},
  getters: {},
  mutations: {},
  actions: {}
})
export default store
  • 然后在main.js文件中引入Vuex
import Vue from 'Vue'
import App from './App.vue'
import store from './store'
const vm = new Vue({
  store: store,
  render: h => h(app)
}).$mount('#app')

Vuex的五个核心属性是什么?

分别是state、getters、mutations、actions、modules。

Vuex中状态存储在哪里,怎样改变它?

存储在state中,改变Vuex中的状态的唯一途径就是显示地提交(commit)mutation。

Vuex中状态对象时,使用时需要注意什么?

因为对象是引用类型,复制后改变属性还是会影响原始数据,这样会改变state里面的状态,是不允许的,所以先用深度克隆复制对象,再修改。

怎么在组件中批量使用Vuex的state状态?

使用mapstate辅助函数,利用对象展开运算符将state混入computed对象中

import { mapState } from 'Vuex'
export default {
  computed: {
    ...mapState(['price', 'number'])
  }
}

Vuex要从state派生一些状态出来,且多个组件使用它,怎么做?

使用getter属性,相当于Vue中的计算属性computed,只有原状态改变派生状态才会改变。getter接收两个参数,第一个是state,第二个是getters(可以用来访问其他getter)。

const store = new Vuex.Store({
  state: {
    price: 10,
    number: 10,
    discount: 0.7
  },## 标题
  getters: {
    total: state => {
      return state.price * state.number
    },
    discountTotal: (state, gettes) => {
      return state.discount * getters.total
    }
  }
})

然后在组件中使用计算属性computed通过this.$store.getters.total这些来访问这些派生状态

computed: {
  total() {
    return this.$store.getters.total
  },
  discountTotal() {
    return this.$store.getters.discountTotal
  }
}

怎么通过getter来实现在组件内可以通过特定条件来获取state的状态?

通过让getter返回一个函数,来实现给getter传参,然后通过参数来进行判断从而获取state中满足要求的状态

const store = new Vuex.Store({
  state: {
    todos: [
      { id : 1, done: true }{ id : 2, done: false }
    ]
  },
  getters: {
    getTodoById: (state) => (id) => {
      return state.todos.find(todo => todo.id === id)
    }
  }
})

然后在组件中可以用计算属性computed通过this.$store.getters.getTodoById(2)来访问这些派生状态。

computed: {
  getTodoById() {
    return this.$store.getters.getTodoById
  }
}
mounted() {
  console.log(this.getTodoById(2).done)  // false
}

在Vuex的state中有个状态number表示数量,在组件中怎样改变它

首先在mutation中注册一个mutation

const store = new Vuex.Store({
  state: {
    number: 10
  },
  mutations: {
    SET_NUMBER(state,data) {
      state.number = data
    }
  } 
})

在组件中使用this.$store.commit提交mutation,改变number。

this.$store.commit('SET_NUMBER', 10)

在Vuex中使用mutation要注意什么

mutation 必须是同步函数

在组件中多次提交同一个mutation,怎么写使用更方便

使用mapMutations辅助函数,在组件中这么使用

import { mapMutations } from 'Vuex'
methods: {
  ...mapMutations({
    setNumber: 'SET_NUMBER'
  })
}

然后调用this.setNumber(10),相当于this.$store.commit('SET_NUMBER', 10)

Vuex中action和mutation有什么区别?

  • action提交的是mutation,而不是直接变更状态;mutation可以直接变更状态。
  • action可以包含任意异步操作;mutation只能是同步操作。
  • 提交方式不同。actions是用this.$store.dispatch('Action_Name',data)来提交;mutation是用this.$store.commit('SET_NUMBER',10)来提交。
  • 接收参数不同,mutation第一个参数是state,而action第一个参数是context。

Vuex中action和mutation有什么相同点?

第二个参数都可以接收外部提交时传来的参数。

Vuex中action通常是异步的,那么如何知道action什么时候结束呢

在action函数中返回Promise,然后再提交的时候用then处理

actions: {
  SET_NUMBER_A({ COMMIT }, data) {
    return new Promise((resolve,reject) => {
      commit('SET_NUMBER', 10)
      resolve()
    })
  }
}
this.$store.dispatch('SET_NUMBER_A').then(() => {
  // ....
})

Vuex中有两个action,分别是actionA和actionB,其内都是异步操作,在actionB要提交actionA,需在actionA处理结束再处理其他操作,怎么实现?

利用ES6的asyncawait来实现。

actions: {
  async actionA({ commit }) { ... },
  async actionB({ dispatch }) { await dispatch('actionA') }
}

在模块中,getter和mutation接收的第一个参数state,是全局的还是模块的?

第一个参数state是模块的state,也就是局部的state。

在组件中怎么访问Vuex模块中的getter和state,怎么提交mutation和action?

通过this.$store.gettersthis.$store.state来访问模块中的getter和state。
通过this.$store.commit('mutationA',data)提交模块中的mutation
通过this.$store.dispatch('action',data)提交模块中的action

怎么在带命名空间的模块内提交全局的mutation和action?

将{ root: true }作为第三个参数传给 dispatch 或commit 即可。

this.$store.commit('mutationA', data, { root: true })
this.$store.dispatch('actionA', data, { root: true })

在v-model上怎么用Vuex中的state的值?

<input v-model="message" >
computed: {
  message: {
    get() {
      return this.$store.state.message
    },
    set(value) {
      this.$store.commit('updateMessage', value)
    }
  }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值