资讯专栏INFORMATION COLUMN

手把手教你学vue-4(vuex)

Hujiawei / 2594人阅读

摘要:管理统一组件状态。映射到组件将映射为也支持载荷将映射为将映射为概念允许我们将分割成模块。每个模块拥有自己的类似里面的针对每个组件对应的状态做处理的状态的状态对于模块内部的,局部状态通过暴露出来,根节点状态则为。

1.首先明白vuex是做什么用的。
管理统一组件状态state。每个应用将仅仅包含一个 store 实例。单一状态树让我们能够直接地定位任一特定的状态片段,在调试的过程中也能轻易地取得整个当前应用状态的快照。
2.如何实现 vuex有几个对象

state =>mapState

getters =>mapGetters

actions =>mapActions

mutations => mapMutations

moudle

3.state (注入store)

Vuex 通过 store 选项,提供了一种机制将状态从根组件“注入”到每一个子组件中(需调用 Vue.use(Vuex)):

const app = new Vue({
  el: "#app",
  // 把 store 对象提供给 “store” 选项,这可以把 store 的实例注入所有的子组件
  store,
  components: { Counter },
  template: `
    
` })

通过在根实例中注册 store 选项,该 store 实例会注入到根组件下的所有子组件中,且子组件能通过 this.$store 访问到
当我们需要时刻跟踪状态值的变化时,可以通过组件的计算机属性来确定,然后使用mapState.方法来映射。

computed: {
  localComputed () { /* ... */ },
  // 使用对象展开运算符将此对象混入到外部对象中
  ...mapState({
    // ...
  })
}
4.getters (都是方法)
Vuex 允许我们在 store 中定义“getter”(可以认为是 store 的计算属性)。就像计算属性一样,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算。

Getter 接受 state 作为第一个参数

mapGetters 辅助函数仅仅是将 store 中的 getter

import { mapGetters } from "vuex"
export default {
  computed: {
  // 使用对象展开运算符将 getter 混入 computed 对象中
    ...mapGetters([
      "doneTodosCount",
      "anotherGetter",
      // ...
    ])
  }
}
5. mutation
更改 Vuex 的 store 中的状态的唯一方法是提交 mutation,调用方法store.commit("increment")

注意点

使用常量替代 Mutation 事件类型

export const SOME_MUTATION = "SOME_MUTATION"
// store.js
import Vuex from "vuex"
import { SOME_MUTATION } from "./mutation-types"

const store = new Vuex.Store({
  state: { ... },
  mutations: {
    // 我们可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名
    [SOME_MUTATION] (state) {
      // mutate state
    }
  }
})

Mutation 必须是同步函数

mapMutations

   import { mapMutations } from "vuex"
   export default {
 // ...
 methods: {
   ...mapMutations([
     "increment", // 将 `this.increment()` 映射为 `this.$store.commit("increment")`
   
     // `mapMutations` 也支持载荷:
     "incrementBy" // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit("incrementBy", amount)`
   ]),
   ...mapMutations({
     add: "increment" // 将 `this.add()` 映射为 `this.$store.commit("increment")`
   })
 }
   }

6.Action

Action 提交的是 mutation,而不是直接变更状态。

Action 可以包含任意异步操作。

context 不是 store 实例本身
Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象,因此你可以调用 context.commit 提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。
actions: {
 increment ({ commit }) {
   commit("increment")
 }
}
mapActions 映射到组件
import { mapActions } from "vuex"

export default {
  // ...
  methods: {
    ...mapActions([
      "increment", // 将 `this.increment()` 映射为 `this.$store.dispatch("increment")`

      // `mapActions` 也支持载荷:
      "incrementBy" // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch("incrementBy", amount)`
    ]),
    ...mapActions({
      add: "increment" // 将 `this.add()` 映射为 `this.$store.dispatch("increment")`
    })
  }
}
7. Module
概念
Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter.类似redux里面的reducer 针对每个组件对应的state状态做处理
const moduleA = {
  state: { ... },
  mutations: { ... },
  actions: { ... },
  getters: { ... }
}

const moduleB = {
  state: { ... },
  mutations: { ... },
  actions: { ... }
}

const store = new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB
  }
})

store.state.a // -> moduleA 的状态
store.state.b // -> moduleB 的状态

rootState

对于模块内部的 action,局部状态通过 context.state 暴露出来,根节点状态则为 context.rootState。
rootState 可以获取到所有mudule里面的state值

const moduleA = {
  // ...
  actions: {
    incrementIfOddOnRootSum ({ state, commit, rootState }) {
      if ((state.count + rootState.count) % 2 === 1) {
        commit("increment")
      }
    }
  }
}

这个还是要多看官方的demo

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/93929.html

相关文章

  • 把手你学Dapr

    摘要:配置配置使用概率抽样。采样率定义了对跟踪跨度进行采样的概率,其值可以介于和含之间。例如,以下配置对象将采样率更改为即每个跨度都被采样,并使用协议将跟踪发送到位于的服务器文件路径注将采样率更改为会完全禁用跟踪。目录手把手教你学Dapr - 1. .Net开发者的大时代手把手教你学Dapr - 2. 必须知道的概念手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序手把手教你学Da...

    qqlcbb 评论0 收藏0
  • 把手你学Vue-2(组件开发)

    摘要:组件声明组件分为全局的和局部的。父组件通过给子组件下发数据,子组件通过事件给父组件发送消息。以下实例中子组件已经和它外部完全解耦了。 1.vue 组件-声明 组件分为全局的和局部的。 全局声明 Vue.component(tagName, options) ** 引用组件 // 注册 Vue.comp...

    lansheng228 评论0 收藏0
  • 把手你学Vue-2(组件开发)

    摘要:组件声明组件分为全局的和局部的。父组件通过给子组件下发数据,子组件通过事件给父组件发送消息。以下实例中子组件已经和它外部完全解耦了。 1.vue 组件-声明 组件分为全局的和局部的。 全局声明 Vue.component(tagName, options) ** 引用组件 // 注册 Vue.comp...

    Null 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<