详解Vue This$Store总结

yizhihongxing

详解Vue This.$Store总结

Vue.js是一款轻量级、高效的JavaScript框架,它本身没有提供全局状态管理机制。但是,vue系列的开发者们为我们写了一个插件——VueX,用来解决这个问题。本文将详细讲解Vue This.$Store总结。

VueX是什么?

VueX是一个用于state管理的应用程序状态管理模式。它使用了集中式存储管理数据,并以可预测方式处理应用程序的状态。通过VueX,我们可以方便地在不同的组件中共享状态,提高开发效率。

如何在Vue中使用VueX?

下面是使用VueX的基本步骤:

  1. 安装VueX
npm install vuex --save
  1. 在main.js文件中加入代码:
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
  1. 创建store对象
const store = new Vuex.Store({
  state: {...},
  mutations: {...},
  actions: {...},
  getters: {...}
})
  1. 将store对象注入到Vue实例中
new Vue({
  render: h => h(App),
  store,
}).$mount('#app')
  1. 使用store对象的状态属性

假如我们在store中定义了一个状态属性count,可以使用以下代码在组件中读取该状态属性:

computed: {
  ...mapState({
    count: state => state.count
  })
}

VueX的四大核心概念

State

State即应用程序的状态管理机制。我们将需要存储的数据放到一个全局的store中,每个组件可以通过$store.state来访问该对象的数据。

Mutation

Mutation用来管理状态的修改。改变store中数据的唯一方式就是提交mutation。

mutations: {
  increment (state) {
    state.count++
  }
}

Action

Action类似于mutation,但是Action除了修改状态之外,还可以触发其他的mutation。Action可以包含任意异步操作,而mutation则只能是同步的。

actions: {
  async fetchData ({commit}, url) {
    const data = await api.fetch(url)
    commit('setReslut', data)
  }
}

Getter

Getter用来对数据进行包装或计算。Getter是只读的,只会返回新的数据,不会修改原数据。

getters: {
  doubleCount (state) {
    return state.count * 2
  }
}

在组件中使用VueX

  1. 使用mapState辅助函数

mapState函数,将store中的状态映射为组件的计算属性。

computed: {
  ...mapState({
    count: state => state.count
  })
}
  1. 使用mapGetters辅助函数

mapGetters函数,将store中的getter映射为组件中的计算属性。

computed: {
  ...mapGetters([
    'doubleCount'
  ])
}

示例说明

示例1:使用VueX实现简单计数器

store.js中的代码

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    },
    decrement (state) {
      state.count--
    }
  },
  actions: {
    incrementAsync ({ commit }) {
      setTimeout(() => {
        commit('increment')
      }, 1000)
    }
  },
  getters: {
    doubleCount (state) {
      return state.count * 2
    }
  }
})

export default store

App.vue中的代码

<template>
  <div id="app">
    <h1>{{ count }}</h1>
    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <button @click="incrementAsync">+ Async</button>
    <p>{{ doubleCount }}</p>
  </div>
</template>

<script>
  import { mapState, mapActions, mapGetters } from 'vuex'

  export default {
    name: 'App',
    computed: {
      ...mapState({
        count: state => state.count
      }),
      ...mapGetters([
        'doubleCount'
      ])
    },
    methods: {
      ...mapActions([
        'increment',
        'decrement',
        'incrementAsync'
      ])
    }
  }
</script>

示例2:使用VueX管理网站主题

store.js中的代码

const store = new Vuex.Store({
  state: {
    theme: 'dark'
  },
  mutations: {
    toggleTheme (state) {
      if (state.theme === 'dark') {
        state.theme = 'light'
      } else {
        state.theme = 'dark'
      }
    }
  }
})

export default store

App.vue中的代码

<template>
  <div :class="theme">
    <h1>Hello World</h1>
    <button @click="toggleTheme">Toggle Theme</button>
  </div>
</template>

<script>
  import { mapState, mapMutations } from 'vuex'

  export default {
    computed: {
      ...mapState({
        theme: state => state.theme
      })
    },
    methods: {
      ...mapMutations([
        'toggleTheme'
      ])
    }
  }
</script>

<style>
  .dark {
    background-color: #333;
    color: #fff;
  }

  .light {
    background-color: #fff;
    color: #333;
  }
</style>

以上是Vue This.$Store总结的详细介绍,希望读者们能够掌握VueX的基本使用方法和核心概念,以及如何在组件中使用VueX。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解Vue This$Store总结 - Python技术站

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

相关文章

  • 详解Vue3.0 + TypeScript + Vite初体验

    我来详细讲解“详解Vue3.0 + TypeScript + Vite初体验”的完整攻略。 什么是Vue3.0 Vue3.0是一款流行的前端JavaScript框架,专注于构建用户界面。它提供了一系列的工具和API,使得开发单页应用变得更加容易和高效。Vue3.0采用了一系列的新特性,例如Composition API,优化了性能和代码维护难度。 为什么要使…

    Vue 2023年5月27日
    00
  • Vue filter格式化时间戳时间成标准日期格式的方法

    下面是 “Vue filter格式化时间戳时间成标准日期格式的方法”的完整攻略: 1. 什么是Vue filter? Vue.filter是Vue提供的用于全局过滤器的机制,它可以重用一些常见的文本转换用法(包括格式化时间戳),以确保我们的代码具有更高的可读性、可维护性和可重用性。 下面我们将讲解如何使用Vue filter来格式化时间戳成标准的日期时间格式…

    Vue 2023年5月28日
    00
  • Vue键盘事件用法总结

    Vue键盘事件用法总结 1. 概述 Vue 提供了多种方式监听键盘事件,通过使用修饰符或者组合键来响应各种操作。本文将总结常用的 Vue 键盘事件的用法。 2. 事件修饰符 在 Vue 中,可以使用如下修饰符来监听键盘事件: .enter:按下回车键时触发; .tab:按下 tab 键时触发; .delete:按下删除或退格键时触发; .esc:按下 Esc…

    Vue 2023年5月29日
    00
  • 使用Elemen加上lang=“ts“后编译报错

    当我们在使用Element UI开发Vue项目,并且使用TypeScript等其他语言时,在引入Element UI组件时,需要在script标签中的lang属性指定为ts,例如: <script lang="ts"> import { Component, Vue } from ‘vue-property-decorator…

    Vue 2023年5月28日
    00
  • vue实现倒计时获取验证码效果

    好的。实现倒计时获取验证码效果,需要借助Vue.js框架和JavaScript的setTimeout函数,具体步骤如下: 准备工作 在Vue.js项目中安装Vue.js框架,命令为:npm install vue。 在所需的组件中引入Vue.js框架,比如使用ES6的方式引入:import Vue from ‘vue’。 在数据存储部分新建一个变量来存储倒计…

    Vue 2023年5月29日
    00
  • vue拖拽添加的简单实现

    接下来我将详细讲解如何实现 Vue 拖拽添加的简单实现,包括如何安装依赖、编写代码、添加样式等具体步骤。 步骤一 安装依赖 首先需要在项目中安装 Vue.Draggable 这个库,它是一个适用于 Vue 的 drag-and-drop 库,可以帮助我们在 Vue 项目中轻松地实现拖拽功能。 npm install -S vuedraggable 步骤二 编…

    Vue 2023年5月28日
    00
  • 使用vue-cli(vue脚手架)快速搭建项目的方法

    使用vue-cli(vue脚手架)快速搭建项目可以大大地提高开发的效率,本文将使用详细的步骤和示例来讲解如何使用vue-cli快速搭建项目。 1. 安装vue-cli 首先,需要全局安装vue-cli,可以使用npm来安装: npm install -g vue-cli 2. 创建项目 使用vue-cli创建项目,可以使用以下命令: vue create […

    Vue 2023年5月27日
    00
  • Vue Prop属性功能与用法实例详解

    当我们需要将数据从父组件传递到子组件时,可以使用 Vue 中的 Prop 属性来实现。本文将详细讲解 Vue Prop 属性的功能和用法,并提供两个示例说明。 Prop 属性的基本概念 Prop 属性的作用 Prop 属性用于将数据从父组件传递到子组件,实现组件之间的数据通信。 Prop 属性的传值方式 父组件通过向子组件传递属性 props ,子组件通过 …

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