相关文章推荐
茫然的钥匙  ·  流程设置-条件分支-全浏览器支持的轻量化AS ...·  1 月前    · 
悲伤的墨镜  ·  全局变量的实现方式 | uView - ...·  1 月前    · 
才高八斗的茄子  ·  全局变量的实现方式 | uView 2.0 ...·  1 月前    · 
打篮球的自行车  ·  vue如何获取数组中的键值 • ...·  1 月前    · 
好帅的丝瓜  ·  用 Jest 测试单文件组件 | Vue ...·  6 天前    · 
大方的柑橘  ·  杂记 ...·  1 年前    · 
热情的橡皮擦  ·  将多个存储过程导出到文本文件-腾讯云开发者社 ...·  1 年前    · 
俊逸的西瓜  ·  8. LINQ查询_07_EF Core - 知乎·  2 年前    · 
鼻子大的拖把  ·  数据万象 Glide网络优化(Http ...·  2 年前    · 
干练的水桶  ·  vue.js - ...·  2 年前    · 
Code  ›  Getting Started | Vuex
vue
https://vuex.vuejs.org/guide/
聪明的茶叶
5 月前
Vuex
Guide
API Reference
Release Notes
  • v3.x
  • English
  • 简体中文
  • 日本語
  • Português
GitHub
Guide
API Reference
Release Notes
  • v3.x
  • English
  • 简体中文
  • 日本語
  • Português
GitHub
  • Introduction

    • What is Vuex?
    • Installation
    • Getting Started
      • The Simplest Store
  • Core Concepts

    • State
    • Getters
    • Mutations
    • Actions
    • Modules
  • Advanced

    • Application Structure
    • Composition API
    • Plugins
    • Strict Mode
    • Form Handling
    • Testing
    • Hot Reloading
    • TypeScript Support
  • Migration Guide

    • Migrating to 4.0 from 3.x

Getting Started #

Try this lesson on Scrimba

At the center of every Vuex application is the store . A "store" is basically a container that holds your application state . There are two things that make a Vuex store different from a plain global object:

  1. Vuex stores are reactive. When Vue components retrieve state from it, they will reactively and efficiently update if the store's state changes.

  2. You cannot directly mutate the store's state. The only way to change a store's state is by explicitly committing mutations . This ensures every state change leaves a track-able record, and enables tooling that helps us better understand our applications.

The Simplest Store #

NOTE

We will be using ES2015 syntax for code examples for the rest of the docs. If you haven't picked it up, you should !

After installing Vuex, let's create a store. It is pretty straightforward - just provide an initial state object, and some mutations:

import { createApp } from 'vue'
import { createStore } from 'vuex'
// Create a new store instance.
const store = createStore({
  state () {
    return {
      count: 0
  mutations: {
    increment (state) {
      state.count++
const app = createApp({ /* your root component */ })
// Install the store instance as a plugin
app.use(store)

Now, you can access the state object as store.state , and trigger a state change with the store.commit method:

store.commit('increment')
console.log(store.state.count) // -> 1

In a Vue component, you can access the store as this.$store . Now we can commit a mutation using a component method:

methods: {
  increment() {
    this.$store.commit('increment')
    console.log(this.$store.state.count)
 
推荐文章
茫然的钥匙  ·  流程设置-条件分支-全浏览器支持的轻量化ASP.NET CORE VUE前后端分离工作流程引擎!
1 月前
悲伤的墨镜  ·  全局变量的实现方式 | uView - 多平台快速开发的UI框架 - uni-app UI框架
1 月前
才高八斗的茄子  ·  全局变量的实现方式 | uView 2.0 - 全面兼容 nvue 的 uni-app 生态框架 - uni-app UI 框架
1 月前
打篮球的自行车  ·  vue如何获取数组中的键值 • Worktile社区
1 月前
好帅的丝瓜  ·  用 Jest 测试单文件组件 | Vue Test Utils
6 天前
大方的柑橘  ·  杂记 Unity读取MySql遇到的问题_针对的是netframework,version=v3.5,profile=unity subset-CSDN博客
1 年前
热情的橡皮擦  ·  将多个存储过程导出到文本文件-腾讯云开发者社区-腾讯云
1 年前
俊逸的西瓜  ·  8. LINQ查询_07_EF Core - 知乎
2 年前
鼻子大的拖把  ·  数据万象 Glide网络优化(Http Dns、Quic等)-SDK 文档-文档中心-腾讯云
2 年前
干练的水桶  ·  vue.js - vue项目运行jest时报错 import _Promise from "../../core-js/promise"; - SegmentFault 思否
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号