亮神知识库 亮神知识库
首页
  • 手写代码

    • 手写代码系列
  • 基础知识

    • 基础
    • JS底层
    • CSS
  • 原理
  • 浏览器
  • HTTP
  • 网络安全
  • babel
  • webpack基础
  • webpack进阶
  • Vite
  • TypeScript
  • Vue2
  • Vue3
  • Node基础

    • glob
    • 模块化机制
    • 事件循环
    • KOA2框架原理
    • Node子进程
    • cluster原理(了解)
  • 教育行业2021

    • B端
    • C端
    • 工具
  • 游戏行业2025
  • 刷题
  • 杂(待整理)
  • 学习
  • 面试
  • 实用技巧
  • 心情杂货
  • 年度总结
  • 友情链接
关于
  • 分类
  • 标签
  • 归档
  • 收藏
GitHub (opens new window)

亮神

前程明亮,未来可期
首页
  • 手写代码

    • 手写代码系列
  • 基础知识

    • 基础
    • JS底层
    • CSS
  • 原理
  • 浏览器
  • HTTP
  • 网络安全
  • babel
  • webpack基础
  • webpack进阶
  • Vite
  • TypeScript
  • Vue2
  • Vue3
  • Node基础

    • glob
    • 模块化机制
    • 事件循环
    • KOA2框架原理
    • Node子进程
    • cluster原理(了解)
  • 教育行业2021

    • B端
    • C端
    • 工具
  • 游戏行业2025
  • 刷题
  • 杂(待整理)
  • 学习
  • 面试
  • 实用技巧
  • 心情杂货
  • 年度总结
  • 友情链接
关于
  • 分类
  • 标签
  • 归档
  • 收藏
GitHub (opens new window)
  • Vue2

    • 响应式

    • 模版编译

    • 虚拟DOM

    • 整体流程

    • vuex&vue-router

      • Vuex
        • 传递store
        • 集中式存储
        • getters
        • strict
      • vue-router
  • Vue3

  • Vue原理
  • Vue2
  • vuex&vue-router
0zcl
2021-10-18
目录

Vuex

Vuex是一个用来管理Vuex状态的插件。采用<code>集中式存储</code>管理所有组件的状态。

// 两种方式获取数据
this.$store.state.count 
this.$store.getters.xxx  // 定义的getters

// 修改:action --commit--> mutation --mutate--> state
1
2
3
4
5

带问题看源码,加深理解:vuex官网 (opens new window), vuex使用 (opens new window)

# 传递store

  1. 每个组件内都可以使用<code>this.$store.state.xxx,$store如何挂载到组件每个组件中的?

vuex抛出一个Store类和install函数。vuex插件注册时会执行install方法。install会通过mixin在每个组件的<code>beforeCreate</code>添加$store.

  • 最外层的组件。this.$store为实例vue时,传进来的store
  • 接下来把最外层的$store,一层一层传给组件的$store
// src/index.js
export {
  Store,
  install,
  // ...
}

// src/store.js
export function install (_Vue) {
  // ...
  applyMixin(Vue)
}

// src/mixin.js
function vuexInit () {
  const options = this.$options
  // store injection
  if (options.store) {
    this.$store = typeof options.store === 'function'
      ? options.store()
      : options.store
  } else if (options.parent && options.parent.$store) {
    this.$store = options.parent.$store
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# 集中式存储

  1. 如何理解集中式存储?数据是放在哪里的?

集中式存储就是把要管理的数据集中起来维护,方便管理。实际上会给store._vm实例化一个vue对象,使用响应式来管理数据。当使用<code>this.$store.state.count获取值时,实际上是使用store._vm._data.$$state.count</code>

const state = store.state
// ...
store._vm = new Vue({
  data: {
    $$state: state
  },
  computed
})

export class Store {
  // ...
  get state () {
    return this._vm._data.$$state
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# getters

  1. vuex定义了一个<code>eventOrOdd</code>的count的computed。computed中须是依赖属性才生效,为什么可以使用computed来获取数据?
<div>count is {{ count }}</div>
// ...
computed: {
  count () {
    return store.state.count
  }
}
1
2
3
4
5
6
7

数据是存放到vue实例中的,vuex存放的数据也是响应式数据。count是依赖属性,因此可以使用computed

# strict

  1. 可以通过this.$store.state.xxx = xxx直接修改状态吗?如何禁止直接修改?如何实现禁止修改的?

可以。加上<code>strict:true</code>即可禁止直接修改数据。

当strict设置为true,会对数据使用watch监听数据。当直接修改时,数据发生改变,触发watch监听回调,如果是开发环境,则爆错。

if (store.strict) {
  enableStrictMode(store)
}

function enableStrictMode (store) {
  store._vm.$watch(function () { return this._data.$$state }, () => {
    if (__DEV__) {
      assert(store._committing, `do not mutate vuex store state outside mutation handlers.`)
    }
  }, { deep: true, sync: true })
}

// Error: [vuex] do not mutate vuex store state outside mutation handlers.
1
2
3
4
5
6
7
8
9
10
11
12
13
编辑 (opens new window)
上次更新: 2025/07/17, 07:17:44
过滤器
vue-router

← 过滤器 vue-router→

最近更新
01
2024年
07-20
02
2023年
07-20
03
2022年
07-20
更多文章>
Theme by Vdoing | Copyright © 2025-2025 亮神 | MIT License | 桂ICP备2024034950号 | 桂公网安备45142202000030
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式