Vuex 入门教程

yizhihongxing

Vuex 入门教程

什么是 Vuex?

Vuex 是一个专为 Vue.js 设计的状态管理库。它能以一种可预测的方式管理应用的状态,并使得视图和状态之间的关系更加简单明了。

Vuex 核心概念

State

Vuex 使用单一状态树,即用一个对象包含了全部的应用层级状态。

一个简单的例子:

const store = new Vuex.Store({
  state: {
    count: 0
  }
})

其中 count 就是我们的应用状态,通过 this.$store.state.count 可以访问该状态。

Getter

Getter 可以看做是 store 的计算属性,如果一个状态的值依赖于多个状态,可以用 Getter 来计算。

一个简单的例子:

const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, task: 'todo1', done: false },
      { id: 2, task: 'todo2', done: true },
      { id: 3, task: 'todo3', done: false }
    ]
  },
  getters: {
    doneTodos: state => {
      return state.todos.filter(todo => todo.done)
    }
  }
})

其中 doneTodos 就是我们的 Getter,通过 this.$store.getters.doneTodos 可以访问该计算属性。

Mutation

Mutation 是改变状态的唯一途径,但它必须是同步的方式,任何异步操作都必须放在 Action 中。

一个简单的例子:

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

其中 increment 就是我们的 Mutation,通过 this.$store.commit('increment') 可以调用该 Mutation。

Action

Action 可以看做是对 Mutation 的封装,它可以执行异步操作,并调用多个 Mutation,可以指定异步操作执行成功后调用的 Mutation。

一个简单的例子:

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    increment (context) {
      setTimeout(() => {
        context.commit('increment')
      }, 1000)
    }
  }
})

其中 increment 就是我们的 Action,通过 this.$store.dispatch('increment') 可以调用该 Action。

Vuex 完整示例

以下是一个完整的 TodoList 应用示例,它包含了 Vuex 的所有核心概念。

<template>
  <div>
    <h1>TodoList</h1>
    <input type="text" v-model="newTodo">
    <button @click="addTodo">Add Todo</button>
    <ul>
      <li v-for="todo in filteredTodos" :key="todo.id">
        <input type="checkbox" v-model="todo.done">
        {{ todo.task }}
      </li>
    </ul>
    <h2>Completed todos</h2>
    <ul>
      <li v-for="todo in doneTodos" :key="todo.id">
        {{ todo.task }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  computed: {
    filteredTodos () {
      return this.$store.getters.filteredTodos
    },
    doneTodos () {
      return this.$store.getters.doneTodos
    }
  },
  methods: {
    addTodo () {
      this.$store.dispatch('addTodo', this.newTodo)
      this.newTodo = ''
    }
  },
  data () {
    return {
      newTodo: ''
    }
  }
}
</script>
const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, task: 'todo1', done: false },
      { id: 2, task: 'todo2', done: true },
      { id: 3, task: 'todo3', done: false }
    ]
  },
  getters: {
    filteredTodos (state) {
      return state.todos.filter(todo => !todo.done)
    },
    doneTodos (state) {
      return state.todos.filter(todo => todo.done)
    }
  },
  mutations: {
    addTodo (state, newTodo) {
      state.todos.push({
        id: state.todos.length + 1,
        task: newTodo,
        done: false
      })
    }
  },
  actions: {
    addTodo (context, newTodo) {
      setTimeout(() => {
        context.commit('addTodo', newTodo)
      }, 1000)
    }
  }
})

在上述示例中,我们使用了 Vuex 来管理 TodoList 应用的状态,包括 todos 列表及其状态,添加 Todo 的方法及其异步操作等。其中 store 包含了我们的状态、getters、mutations 和 actions,通过在 Vue 组件内调用 this.$store.state.xxxthis.$store.getters.xxx 等方法来访问状态。

总结

本文讲解了 Vuex 的核心概念及其应用,代码示例基于 Vue 单文件组件进行实现,让初学者能够更好的理解。同时,本文仅讲解了 Vuex 的入门内容,更进一步的内容需要进一步学习。

参考资源

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Vuex 入门教程 - Python技术站

(0)
上一篇 2023年5月27日
下一篇 2023年5月27日

相关文章

  • 10分钟上手vue-cli 3.0 入门介绍

    10分钟上手vue-cli 3.0 入门介绍 什么是vue-cli 3.0 vue-cli是一套通过命令行工具帮助我们快速创建Vue.js项目开发环境的脚手架工具。其3.0版本带来了更好的用户体验和更快的构建速度。 安装vue-cli 3.0 首先,我们需要安装node.js和npm。安装完毕后,打开命令行窗口,输入以下命令进行全局安装vue-cli: np…

    Vue 2023年5月27日
    00
  • VueRouter 原理解读之初始化流程

    VueRouter 是 Vue.js 官方的路由管理器,负责管理我们应用程序的路由。VueRouter 的原理可以分为初始化流程、路由监听、处理路由变化等几个方面。在本次对话中,我将为您详细讲解 VueRouter 的初始化流程。 VueRouter 的初始化流程可以分为四个阶段: 创建 Router 实例 注册组件 解析路由配置 监听路由变化 下面我们分别…

    Vue 2023年5月28日
    00
  • 详解Vue 中 extend 、component 、mixins 、extends 的区别

    让我详细讲解一下“详解Vue 中 extend 、component 、mixins 、extends 的区别”。 extend extend是Vue实例的一个方法,在使用时需要首先通过调用该方法来创建一个构造函数,然后通过该构造函数来创建Vue实例。 示例: // 创建一个名为MyComponent的构造函数 const MyComponent = Vue…

    Vue 2023年5月28日
    00
  • vue中如何使用embed标签PDF预览

    下面我来详细讲解“vue中如何使用embed标签PDF预览”的完整攻略。 一、前置条件 在使用embed标签预览PDF文件前,需要先安装Vue CLI工具,同时安装Vue PDF Viewer插件。 二、使用embed标签预览PDF文件的方法 以下是两种使用embed标签预览PDF文件的方法。 方法一:使用第三方组件库 Vue PDF Viewer是一个Vu…

    Vue 2023年5月28日
    00
  • Vue源码学习之数据初始化

    Vue源码是前端开发中非常重要的一个框架,数据初始化作为Vue的一个重要环节,在Vue的源码学习过程中也必不可少,下面我将带你详细讲解Vue源码学习之数据初始化的完整攻略。 一、数据初始化的作用 在Vue的生命周期中,数据初始化是第一步,也是非常重要的一步。它的主要作用是将模板中的数据与Vue的实例建立绑定关系,并对数据进行响应式处理,从而使得数据的改变能够…

    Vue 2023年5月28日
    00
  • Vue打包后出现一些map文件的解决方法

    问题描述: 在 Vue 项目中打包后,可能会在控制台看到一些类似于以下的提示: Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/js/chunk-6e951050.578deb7c.js.map 这是因为在…

    Vue 2023年5月27日
    00
  • VsCode里的Vue模板的实现

    下面是关于VsCode里的Vue模板的实现的完整攻略。 什么是Vue模板? 在VsCode中,Vue模板是一种代码片段(snippet),简化了Vue.js的常用代码块的编写,让开发人员更加专注于逻辑开发,提高开发效率。 如何在VsCode中使用Vue模板? 安装插件 首先,需要在VsCode中安装支持Vue代码片段的插件,有多种插件可供选择,例如: Vet…

    Vue 2023年5月28日
    00
  • js中什么时候不能使用箭头函数

    使用箭头函数的时候需要注意一些使用限制,下面详细讲解什么时候不能使用箭头函数以及注意事项。 不能使用箭头函数的情况 1. 不能作为构造函数 箭头函数不能作为构造函数,也就是不能使用 new 关键字创建对象。因为箭头函数没有自己的 this,也没有 prototype 属性。所以,如果你尝试使用 “箭头函数” 作为构造函数,则会产生异常。 // 箭头函数不能作…

    Vue 2023年5月28日
    00
合作推广
合作推广
分享本页
返回顶部