vuex-vue2

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

When to use this skill

何时使用此技能

Use this skill whenever the user wants to:
  • Install and set up Vuex in a Vue 2 project
  • Manage application state with Vuex
  • Use Vuex store in Vue components
  • Understand Vuex core concepts (state, getters, mutations, actions)
  • Use Vuex modules for large applications
  • Handle Vuex plugins and devtools
  • Understand Vuex API and methods
  • Troubleshoot Vuex issues
当用户有以下需求时,可使用此技能:
  • 在Vue 2项目中安装和配置Vuex
  • 使用Vuex管理应用状态
  • 在Vue组件中使用Vuex store
  • 理解Vuex核心概念(state、getters、mutations、actions)
  • 在大型应用中使用Vuex modules
  • 处理Vuex插件和开发者工具
  • 理解Vuex API与方法
  • 排查Vuex相关问题

How to use this skill

如何使用此技能

This skill is organized to match the Vuex official documentation structure (https://vuex.vuejs.org/zh/, https://vuex.vuejs.org/zh/guide/, https://vuex.vuejs.org/zh/api/). When working with Vuex:
  1. Identify the topic from the user's request:
    • Installation/安装 →
      examples/guide/installation.md
    • Quick Start/快速开始 →
      examples/guide/quick-start.md
    • Core Concepts/核心概念 →
      examples/core-concepts/
    • Advanced/高级 →
      examples/advanced/
    • API/API 文档 →
      api/
  2. Load the appropriate example file from the
    examples/
    directory:
    Guide (使用指南):
    • examples/guide/intro.md
      - Introduction to Vuex
    • examples/guide/installation.md
      - Installation guide
    • examples/guide/quick-start.md
      - Quick start guide
    • examples/guide/what-is-vuex.md
      - What is Vuex
    Core Concepts (核心概念):
    • examples/core-concepts/state.md
      - State
    • examples/core-concepts/getters.md
      - Getters
    • examples/core-concepts/mutations.md
      - Mutations
    • examples/core-concepts/actions.md
      - Actions
    • examples/core-concepts/modules.md
      - Modules
    Advanced (高级):
    • examples/advanced/plugins.md
      - Plugins
    • examples/advanced/strict-mode.md
      - Strict mode
    • examples/advanced/form-handling.md
      - Form handling
    • examples/advanced/testing.md
      - Testing
    • examples/advanced/hot-reload.md
      - Hot reload
  3. Follow the specific instructions in that example file for syntax, structure, and best practices
    Important Notes:
    • Vuex is for Vue 2.x
    • Store is the central state management
    • State is reactive
    • Mutations are synchronous
    • Actions are asynchronous
    • Each example file includes key concepts, code examples, and key points
  4. Reference API documentation in the
    api/
    directory when needed:
    • api/store-api.md
      - Store API
    • api/state-api.md
      - State API
    • api/getters-api.md
      - Getters API
    • api/mutations-api.md
      - Mutations API
    • api/actions-api.md
      - Actions API
    • api/modules-api.md
      - Modules API
    • api/plugins-api.md
      - Plugins API
  5. Use templates from the
    templates/
    directory:
    • templates/installation.md
      - Installation templates
    • templates/store-setup.md
      - Store setup templates
    • templates/component-usage.md
      - Component usage templates
  1. 从用户请求中确定主题
    • Installation/安装 →
      examples/guide/installation.md
    • Quick Start/快速开始 →
      examples/guide/quick-start.md
    • Core Concepts/核心概念 →
      examples/core-concepts/
    • Advanced/高级 →
      examples/advanced/
    • API/API 文档 →
      api/
  2. examples/
    目录加载对应的示例文件
    Guide (使用指南):
    • examples/guide/intro.md
      - Vuex介绍
    • examples/guide/installation.md
      - 安装指南
    • examples/guide/quick-start.md
      - 快速开始指南
    • examples/guide/what-is-vuex.md
      - 什么是Vuex
    Core Concepts (核心概念):
    • examples/core-concepts/state.md
      - State
    • examples/core-concepts/getters.md
      - Getters
    • examples/core-concepts/mutations.md
      - Mutations
    • examples/core-concepts/actions.md
      - Actions
    • examples/core-concepts/modules.md
      - Modules
    Advanced (高级):
    • examples/advanced/plugins.md
      - Plugins
    • examples/advanced/strict-mode.md
      - 严格模式
    • examples/advanced/form-handling.md
      - 表单处理
    • examples/advanced/testing.md
      - 测试
    • examples/advanced/hot-reload.md
      - 热重载
  3. 遵循示例文件中的语法、结构与最佳实践说明
    重要提示:
    • Vuex适用于Vue 2.x
    • Store是集中式状态管理容器
    • State是响应式的
    • Mutations是同步操作
    • Actions是异步操作
    • 每个示例文件都包含核心概念、代码示例和关键点
  4. 必要时参考
    api/
    目录中的API文档
    :
    • api/store-api.md
      - Store API
    • api/state-api.md
      - State API
    • api/getters-api.md
      - Getters API
    • api/mutations-api.md
      - Mutations API
    • api/actions-api.md
      - Actions API
    • api/modules-api.md
      - Modules API
    • api/plugins-api.md
      - Plugins API
  5. 使用
    templates/
    目录中的模板
    :
    • templates/installation.md
      - 安装模板
    • templates/store-setup.md
      - Store配置模板
    • templates/component-usage.md
      - 组件使用模板

1. Understanding Vuex

1. 理解Vuex

Vuex is a state management pattern and library for Vue.js applications. It serves as a centralized store for all the components in an application.
Key Concepts:
  • Store: Centralized state container
  • State: Application state (data)
  • Getters: Computed properties for store
  • Mutations: Synchronous state changes
  • Actions: Asynchronous operations
  • Modules: Store organization
Vuex是Vue.js应用的状态管理模式与库,作为应用中所有组件的集中式存储。
核心概念:
  • Store: 集中式状态容器
  • State: 应用状态(数据)
  • Getters: Store的计算属性
  • Mutations: 同步状态变更
  • Actions: 异步操作
  • Modules: Store的组织方式

2. Installation

2. 安装

Using npm:
bash
npm install vuex@3
Using yarn:
bash
yarn add vuex@3
Using CDN:
html
<script src="https://unpkg.com/vuex@3"></script>
使用npm:
bash
npm install vuex@3
使用yarn:
bash
yarn add vuex@3
使用CDN:
html
<script src="https://unpkg.com/vuex@3"></script>

3. Basic Setup

3. 基础配置

javascript
// store/index.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    }
  }
})

export default store
javascript
// main.js
import Vue from 'vue'
import store from './store'

new Vue({
  store,
  render: h => h(App)
}).$mount('#app')
javascript
// store/index.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    }
  }
})

export default store
javascript
// main.js
import Vue from 'vue'
import store from './store'

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

Doc mapping (one-to-one with official documentation)

文档映射(与官方文档一一对应)

Examples and Templates

示例与模板

This skill includes detailed examples organized to match the official documentation structure. All examples are in the
examples/
directory (see mapping above).
To use examples:
  • Identify the topic from the user's request
  • Load the appropriate example file from the mapping above
  • Follow the instructions, syntax, and best practices in that file
  • Adapt the code examples to your specific use case
To use templates:
  • Reference templates in
    templates/
    directory for common scaffolding
  • Adapt templates to your specific needs and coding style
本技能包含与官方文档结构匹配的详细示例,所有示例都在
examples/
目录中(见上方映射)。
使用示例:
  • 从用户请求中确定主题
  • 根据上方映射加载对应的示例文件
  • 遵循文件中的说明、语法与最佳实践
  • 根据具体场景调整代码示例
使用模板:
  • 参考
    templates/
    目录中的通用模板进行脚手架搭建
  • 根据具体需求和编码风格调整模板

API Reference

API参考

Detailed API documentation is available in the
api/
directory, organized to match the official Vuex API documentation structure (https://vuex.vuejs.org/zh/api/):
详细的API文档位于
api/
目录中,组织结构与Vuex官方API文档一致(https://vuex.vuejs.org/zh/api/):

Store API (
api/store-api.md
)

Store API (
api/store-api.md
)

  • Store constructor options
  • Store instance properties
  • Store instance methods
  • Store构造函数选项
  • Store实例属性
  • Store实例方法

State API (
api/state-api.md
)

State API (
api/state-api.md
)

  • State definition
  • State access
  • State reactivity
  • State定义
  • State访问
  • State响应式

Getters API (
api/getters-api.md
)

Getters API (
api/getters-api.md
)

  • Getter definition
  • Getter access
  • Getter arguments
  • Getter定义
  • Getter访问
  • Getter参数

Mutations API (
api/mutations-api.md
)

Mutations API (
api/mutations-api.md
)

  • Mutation definition
  • Mutation commit
  • Mutation payload
  • Mutation定义
  • Mutation提交
  • Mutation负载

Actions API (
api/actions-api.md
)

Actions API (
api/actions-api.md
)

  • Action definition
  • Action dispatch
  • Action context
  • Action定义
  • Action分发
  • Action上下文

Modules API (
api/modules-api.md
)

Modules API (
api/modules-api.md
)

  • Module definition
  • Module namespacing
  • Module registration
  • Module定义
  • Module命名空间
  • Module注册

Plugins API (
api/plugins-api.md
)

Plugins API (
api/plugins-api.md
)

  • Plugin definition
  • Plugin usage
  • Built-in plugins
To use API reference:
  1. Identify the API you need help with
  2. Load the corresponding API file from the
    api/
    directory
  3. Find the API signature, parameters, return type, and examples
  4. Reference the linked example files for detailed usage patterns
  5. All API files include links to relevant example files in the
    examples/
    directory
  • Plugin定义
  • Plugin使用
  • 内置插件
使用API参考:
  1. 确定需要帮助的API
  2. api/
    目录加载对应的API文件
  3. 查找API签名、参数、返回类型和示例
  4. 参考链接的示例文件获取详细使用模式
  5. 所有API文件都包含指向
    examples/
    目录中相关示例文件的链接

Best Practices

最佳实践

  1. Use mutations for synchronous changes: Mutations must be synchronous
  2. Use actions for async operations: Actions can contain async operations
  3. Keep state normalized: Avoid nested state structures
  4. Use modules for large apps: Organize store with modules
  5. Use getters for computed state: Derive state with getters
  6. Follow naming conventions: Use consistent naming patterns
  7. Use TypeScript: Leverage TypeScript for type safety
  1. 使用mutations处理同步变更:Mutations必须是同步的
  2. 使用actions处理异步操作:Actions可包含异步操作
  3. 保持状态规范化:避免嵌套状态结构
  4. 大型应用使用modules:用modules组织Store
  5. 使用getters处理计算状态:通过getters派生状态
  6. 遵循命名规范:使用一致的命名模式
  7. 使用TypeScript:利用TypeScript保证类型安全

Resources

资源

Keywords

关键词

Vuex, vuex, Vue 2, state management, 状态管理, store, state, getters, mutations, actions, modules, 存储, 状态, 获取器, 变更, 动作, 模块, Vuex store, Vuex state, Vuex getters, Vuex mutations, Vuex actions, Vuex modules, Vuex plugins, centralized state, reactive state, synchronous mutations, asynchronous actions
Vuex, vuex, Vue 2, state management, 状态管理, store, state, getters, mutations, actions, modules, 存储, 状态, 获取器, 变更, 动作, 模块, Vuex store, Vuex state, Vuex getters, Vuex mutations, Vuex actions, Vuex modules, Vuex plugins, centralized state, reactive state, synchronous mutations, asynchronous actions

能力边界

能力边界

✅ 适用场景

✅ 适用场景

  • 当你需要使用此技能对应的技术栈时
  • 当项目需要遵循最佳实践时
  • 当需要快速上手或深入理解核心概念时
  • 当你需要使用此技能对应的技术栈时
  • 当项目需要遵循最佳实践时
  • 当需要快速上手或深入理解核心概念时

⚠️ 需要注意

⚠️ 需要注意

  • 复杂业务逻辑需要结合具体场景调整
  • 性能优化需要根据实际数据量评估
  • 复杂业务逻辑需要结合具体场景调整
  • 性能优化需要根据实际数据量评估

❌ 不适用场景

❌ 不适用场景

  • 不相关的技术栈或框架
  • 需要完全自定义的特殊场景
  • 不相关的技术栈或框架
  • 需要完全自定义的特殊场景

常见陷阱 (Gotchas)

常见陷阱 (Gotchas)

  1. 版本兼容性:注意框架版本与依赖库的兼容性,不同版本 API 可能有差异
  2. 配置文件格式:配置文件格式错误是最常见的问题,建议使用编辑器的语法检查
  3. 环境变量:确保所有必要的环境变量已正确设置,敏感信息不要硬编码
  4. 依赖冲突:多版本共存时注意依赖冲突,使用 lock 文件锁定版本
  5. 性能陷阱:大数据量场景下注意性能优化,避免 N+1 查询等常见问题
  1. 版本兼容性:注意框架版本与依赖库的兼容性,不同版本API可能有差异
  2. 配置文件格式:配置文件格式错误是最常见的问题,建议使用编辑器的语法检查
  3. 环境变量:确保所有必要的环境变量已正确设置,敏感信息不要硬编码
  4. 依赖冲突:多版本共存时注意依赖冲突,使用lock文件锁定版本
  5. 性能陷阱:大数据量场景下注意性能优化,避免N+1查询等常见问题

使用流程

使用流程

Step 1: 环境准备

Step 1: 环境准备

确保开发环境已安装必要的依赖和工具。
确保开发环境已安装必要的依赖和工具。

Step 2: 配置初始化

Step 2: 配置初始化

根据项目需求进行基础配置。
根据项目需求进行基础配置。

Step 3: 核心功能使用

Step 3: 核心功能使用

按照示例代码实现核心功能。
按照示例代码实现核心功能。

Step 4: 测试验证

Step 4: 测试验证

运行测试确保功能正常。
运行测试确保功能正常。

Step 5: 部署上线

Step 5: 部署上线

完成开发后进行部署和监控。
完成开发后进行部署和监控。