</style>ue学习之Vuex的使用详解

目录
简介
优缺点
优点
缺点
使用场景
示例
安装Vuex并引入
1.安装vuex
2.编写vuex的store
3.main.js引入CounterStore.js
业务代码
测试
简介
说明

本文介绍Vue的插件:Vuex。包括:优缺点、使用场景、示例。

官网

官网文档

API vuex-store

优缺点
优点
1.响应式

属于 vue 生态一环,,能够触发响应式的渲染页面更新。 (localStorage 就不会)

2.可预测的方式改变数据

避免数据污染

3.无需转换数据

JS 原生的数据对象写法(不需要做转换)。(localStorage 需要做转换)

缺点
1.复杂

适合大应用,不适合小型应用

2.不能持久化(刷新页面后vuex中的state会变为初始状态)

解决方案

结合localStorage

vuex-persistedstate插件

使用场景
当我们多个页面需要共享数据时就可以使用Vuex。

实际开发中我们一般在下面这种情况使用它:

当前登录用户的信息

购物车的信息

收藏的信息

用户的地理位置

示例
本处用计数器来测试:一个组件修改计数器的值,其余两个不相关组件可以监测到时计数器值的改变。

做法:一个组件(ComponentA)将数据共享给另外两个不相关组件(ComponentB和ComponentC),外部用Parent.vue放置这三个组件。

安装Vuex并引入
1.安装vuex
在工程目录下执行:npm install vuex

2.编写vuex的store
创建目录store,在其下边创建CounterStore.js,内容如下:ue学习之Vuex的使用详解

目录
简介
优缺点
优点
缺点
使用场景
示例
安装Vuex并引入
1.安装vuex
2.编写vuex的store
3.main.js引入CounterStore.js
业务代码
测试
简介
说明

本文介绍Vue的插件:Vuex。包括:优缺点、使用场景、示例。

官网

官网文档

API vuex-store

优缺点
优点
1.响应式

属于 vue 生态一环,,能够触发响应式的渲染页面更新。 (localStorage 就不会)

2.可预测的方式改变数据

避免数据污染

3.无需转换数据

JS 原生的数据对象写法(不需要做转换)。(localStorage 需要做转换)

缺点
1.复杂

适合大应用,不适合小型应用

2.不能持久化(刷新页面后vuex中的state会变为初始状态)

解决方案

结合localStorage

vuex-persistedstate插件

使用场景
当我们多个页面需要共享数据时就可以使用Vuex。

实际开发中我们一般在下面这种情况使用它:

当前登录用户的信息

购物车的信息

收藏的信息

用户的地理位置

示例
本处用计数器来测试:一个组件修改计数器的值,其余两个不相关组件可以监测到时计数器值的改变。

做法:一个组件(ComponentA)将数据共享给另外两个不相关组件(ComponentB和ComponentC),外部用Parent.vue放置这三个组件。

安装Vuex并引入
1.安装vuex
在工程目录下执行:npm install vuex

2.编写vuex的store
创建目录store,在其下边创建CounterStore.js,内容如下:
import Vue from ‘vue’;
import Vuex from ‘vuex’;

  1. Vue.use(Vuex);
  2. const couterStore = new Vuex.Store(
  3. {
  4. state: {
  5. count1: 0,
  6. count2: 0,
  7. },
  8. mutations: {
  9. increment1(state) {
  10. state.count1++;
  11. },
  12. decrement1(state) {
  13. state.count1--;
  14. },
  15. increment2: state => state.count2++,
  16. decrement2: state => state.count2--,
  17. }
  18. }
  19. );
  20. export default couterStore

3.main.js引入CounterStore.js
// The Vue build version to load with the import command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from ‘vue’
import App from ‘./App’
import router from ‘./router’
import CouterStore from ‘./store/CounterStore’

  1. Vue.config.productionTip = false
  2. /* eslint-disable no-new */
  3. new Vue({
  4. el: '#app',
  5. router,
  6. store: CouterStore,
  7. components: { App },
  8. template: '<App/>'
  9. })

按照JS语法,key与value重名时可以简写:(很多教程这么写)
// The Vue build version to load with the import command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from ‘vue’
import App from ‘./App’
import router from ‘./router’
import store from ‘./store/CounterStore’

  1. Vue.config.productionTip = false
  2. /* eslint-disable no-new */
  3. new Vue({
  4. el: '#app',
  5. router,
  6. store,
  7. components: { App },
  8. template: '<App/>'
  9. })

业务代码
代码

ComponentA.vue
<template>
<div class="container">
<h3>ComponentA</h3>
<button @click="increment1">增加:第1个计数器</button>
<button @click="decrement1">减少:第1个计数器</button><br><br>
<button @click="increment2">增加:第2个计数器</button>
<button @click="decrement2">减少:第2个计数器</button>
</div>
</template>

  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. count1: 0,
  6. count2: 0,
  7. }
  8. },
  9. methods:{
  10. increment1() {
  11. this.$store.commit('increment1')
  12. },
  13. decrement1() {
  14. this.$store.commit('decrement1')
  15. },
  16. increment2() {
  17. this.$store.commit('increment2')
  18. },
  19. decrement2() {
  20. this.$store.commit('decrement2')
  21. }
  22. }
  23. }
  24. </script>
  25. <style scoped>
  26. .container {
  27. margin: 20px;
  28. border: 2px solid blue;
  29. padding: 20px;
  30. }
  31. </style>

ComponentB.vue
<template>
<div class="container">
<h3>ComponentB</h3>
计数器的值:{{msg}}
<!--也可以这么写:-->
<!--计数器的值:{{this.$store.state.count1}}-->
</div>
</template>
Parent.vue
<template>
<div class="outer">
<h3>父组件</h3>
<component-a></component-a>
<component-b></component-b>
<component-c></component-c>

  1. </div>
  2. </template>
  3. <script>
  4. import ComponentA from "./ComponentA";
  5. import ComponentB from "./ComponentB";
  6. import ComponentC from "./ComponentC";
  7. export default {
  8. name: 'Parent',
  9. components: {ComponentA, ComponentB, ComponentC},
  10. data() {
  11. return {
  12. name: 'Tony',
  13. age: 20,
  14. phoneNumber: '1234567890'
  15. }
  16. }
  17. }
  18. </script>
  19. <style scoped>
  20. .outer {
  21. margin: 20px;
  22. border: 2px solid red;
  23. padding: 20px;
  24. }
  25. </style>
  26. <script>
  27. export default {
  28. computed:{
  29. msg() {
  30. return this.$store.state.count1;
  31. }
  32. }
  33. }
  34. </script>
  35. <style scoped>
  36. .container {
  37. margin: 20px;
  38. border: 2px solid blue;
  39. padding: 20px;
  40. }
  41. </style>

路由

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import Parent from "../components/Parent";
  4. Vue.use(Router)
  5. export default new Router({
  6. routes: [
  7. {
  8. path: '/parent',
  9. name: 'Parent',
  10. component: Parent,
  11. }
  12. ],
  13. })

测试
访问: http://localhost:8080/

到此这篇关于Vue学习之Vuex的使用详解的文章就介绍到这了,更多相关Vuex使用内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

更多相关文章

  1. vue 组件化
  2. 前端 UI 样式:01Vue.js组件样式属性 scoped 问题及解决方法
  3. Android(安卓)12上焕然一新的小组件:美观、便捷和实用
  4. android 自定义基于组件的注册用户对话框
  5. 为android封装的百度定位组件
  6. Android(安卓)四大组件之 Activity
  7. 自定义android循环拖动组件
  8. 简单理解vuex
  9. Android学习笔记之自定义UI组件

随机推荐

  1. Python网页静态爬虫
  2. Python学习系列(六)(模块)
  3. 校招录取之后前往公司实习,现在感觉自己前
  4. 如何将dict转换为spark map输出
  5. 当使用一个传送到另一个的python文件时,我
  6. matplotlib绘制符合论文要求的图片
  7. classmethod,staticmethod,还有类里面一般
  8. Python文件操作大全,随机删除文件夹内的任
  9. 获取错误“ValueError:int()的无效文字,基数
  10. C++各大有名库的介绍——准标准库Boost