首页 > 其他分享 >前端【Vuex】【使用介绍】

前端【Vuex】【使用介绍】

时间:2024-04-08 22:26:11浏览次数:15  
标签:vue Vuex 前端 介绍 sex state import vuex store

1、组件下载

  vue 与vuex的版本对应关系:
     Vue 2 匹配的 Vuex 3
     Vue 3 匹配的 Vuex 4
  Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式 + 库。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。
  官方网站:https://vuex.vuejs.org/zh/
1 // npm
2 npm install vuex@next --save
3 
4 // yarn 
5 yarn add vuex@next --save

2、项目中定义及配置

  在src目录下新增store目录,创建index.js文件,内容如下

 1 // 1. 导入所需的包
 2 import Vue from 'vue'
 3 import Vuex from 'vuex'
 4 // 2. 将Vuex注册为Vue的插件
 5 Vue.use(Vuex)
 6 // 3. 创建 store 实例对象
 7 const store = new Vuex.Store({
 8   // 配置vuex
 9   strict: true, // 开启严格模式,防止小白在组件中使用的时候直接修改state数据
10   // state 用于存储数据(存储状态) (vuex状态管理)
11   state: {
12     sex: 23,
13     uname: 'badWoman',
14     list: [
15       { id: 1, name: '吃饭', isDone: true },
16       { id: 2, name: '睡觉', isDone: false },
17       { id: 3, name: '听歌', isDone: true }
18     ]
19   },
20   // mutations 是 vuex 中"唯一"可以修改 state 数据的方法, 不支持异步更新,只能放同步代码
21   mutations: {
22     abc (state, n) {
23       state.sex = n
24     }
25   },
26   // actions 里面放异步方法
27   actions: {
28     newSex (store, n) {
29       setTimeout(() => {
30         store.commit('abc', n) // 只要是修改state中内容, 都通过mutations中的方法进行操作
31       }, 3000)
32     }
33   },
34   // getters 是vuex中的计算属性(和组件中的计算属性意义一样,但是不支持set修改)
35   // 为了方便获取state中的数据;插件作者会给每个计算属性方法,传递一个 state 参数
36   getters: {
37     ccc (state) {
38       return state.sex * state.sex
39     },
40     all (state) {
41       return state.list.every(item => item.isDone === true)
42     }
43   }
44 })
45 // 4. 导出 store 对象
46 export default store

  main.js

 1 import Vue from 'vue'
 2 import App from './App.vue'
 3 // 导入定义的store目录下的index.js文件
 4 // import store from '@/store/index.js' // 如果导入某个目录下的index文件, 则可以省略index
 5 import store from '@/store'
 6 Vue.config.productionTip = false
 7 
 8 new Vue({
 9   store, // 将导入的store添加到vue实例
10   render: h => h(App)
11 }).$mount('#app')

  组件AnyOne.vue

 1 <template>
 2   <div>
 3     <h2>随便一个组件-1</h2>
 4     <!-- 使用vuex中状态, state中的uname -->
 5     <p>{{ $store.state.uname }}</p>
 6     <!-- 使用vuex中状态, state中的sex -->
 7     <p>{{ $store.state.sex }}</p>
 8     <!-- 使用vuex中配置的计算属性 -->
 9     <p>{{ $store.getters.ccc }}</p>
10     <p>{{ $store.getters.all }}</p>
11 
12     <!-- 调用mutations中定义的修改state的方法abc, 并传递参数5000 -->
13     <button @click="$store.commit('abc', 5000)">更新sex</button>
14     <br />
15     <!-- 调用actions中定义的异步方法newSex,并传递参数20 -->
16     <button @click="$store.dispatch('newSex', 20)">3s后更新sex</button>
17   </div>
18 </template>
19 
20 <script>
21 export default {}
22 </script>
23 <style lang="less" scoped></style>

  组件AnyTwo.vue

 1 <template>
 2   <div>
 3     <h2>随便一个组件-2</h2>
 4     <!-- 使用计算属性 -->
 5     <!-- <P>{{ sex }}</P> -->
 6     <p>{{ uname }}</p>
 7     <P>{{ nianLing }}</P>
 8     <P>{{ uname }}</P>
 9     <P>{{ ccc }}</P>
10     <P>{{ all }}</P>
11     <button @click="abc(100)">更新年龄</button>
12     <br />
13     <button @click="newSex(200)">三秒后更新年龄</button>
14   </div>
15 </template>
16 
17 <script>
18 // 导入vuex中提供的一些方法
19 import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
20 export default {
21   // data () {
22   //   return {
23   //     sex: 1777
24   //   }
25   // },
26   computed: {
27     // mapState提供一种简写方式:  ...mapState() 传想要获取的状态信息, 拿到当前页面作为计算属性使用
28     // ...mapState(['sex', 'uname', 'list']),   // 数组写法
29 
30     // 给从state中获取的状态取别名,防止与当前页面重名,作为当前页面的计算属性
31     ...mapState({ nianLing: 'sex', uname: 'uname', list: 'list' }),  // 对象写法, 相当于取别名, 防止与data中的变量重名
32 
33     // 将vuex中的getters拿到当前页面作为当前页面的计算属性
34     ...mapGetters(['ccc', 'all'])
35   },
36   methods: {
37     // 将vuex中mutations定义的方法拿到当前页面作为当前页面的方法
38     ...mapMutations(['abc']),
39     ...mapActions(['newSex'])
40   }
41 }
42 </script>
43 <style lang="less" scoped></style>

  根组件App.vue

 1 <template>
 2   <div>
 3     <AnyOne></AnyOne>
 4     <hr />
 5     <AnyTwo></AnyTwo>
 6   </div>
 7 </template>
 8 
 9 <script>
10 import AnyOne from './components/AnyOne.vue'
11 import AnyTwo from './components/AnyTwo.vue'
12 export default {
13   data () {
14     return {}
15   },
16   components: {
17     AnyOne,
18     AnyTwo
19   }
20 }
21 </script>
22 <style lang="less" scoped></style>

 

3、使用

 

4、简化调用数据

  import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
  通过vuex中的一些方法将定义的store中的内容拿到当前页面

 

标签:vue,Vuex,前端,介绍,sex,state,import,vuex,store
From: https://www.cnblogs.com/wang1001/p/18122722

相关文章

  • xshell安装和连接 bash shell 介绍和使用
     xshell安装和连接           在官网上注册一下可以选择学习来用的,是免费的但是差一些只有4个teble页 链接centos,将centos开启 在xshell中 找不到可以在文件夹里          #重启网卡systemctlrestartnetw......
  • 【Linux系统编程】libevent库介绍与安装
    libevent库介绍与安装libevent介绍libevent是一个异步事件处理软件函式库。libevent是一个提供异步事件通知的软件库。libevent提供了一组应用程序编程接口(API),libeventAPI提供的机制允许开发者为事件注册回调函数,例如文件描述符上的发生了特定事件或者等待特定事件超时,接收到......
  • C++中lambda表达式介绍
    c++在c++11标准中引入了lambda表达式,一般用于定义匿名函数,使得代码更加灵活简洁。lambda表达式与普通函数类似,也有参数列表、返回值类型和函数体,只是它的定义方式更简洁,并且可以在函数内部定义。什么是Lambda表达式最常见的lambda的表达式写法如下autoplus=[](intv1,int......
  • maven中 Uber-jar 名词介绍
    Uber-jar在maven的一些文档中我们会发现"uber-jar"这个术语,许多人看到后感到困惑。其实在很多编程语言中会把super叫做uber(因为suber可能是关键字),这是上世纪80年代开始流行的,比如管superman叫uberman。所以uber-jar从字面上理解就是super-jar,这样的jar不但包含自......
  • vuex分了多个模块,利用语法糖调用不同模块里的方法
    //store/modules/a.jsexportdefault{state:{...},getters:{...},mutations:{...},actions:{...}}//store/modules/b.jsexportdefault{state:{...},getters:{...},mutations:{...},actions:{...}}//store/in......
  • 常用Java代码混淆工具介绍及比较
     ......
  • 冰球大莽斗上线时间+配置介绍+联机加速器推荐
    冰球大莽斗是一款Firestoke发行,RageCureGames开发的多人欢乐体育竞技游戏,玩家可以选择射手、莽卫或者边锋三个不同职业,体验丰富有趣的冰球运动。上线时间本作将于4月11日登陆steam平台,目前游戏已上架steam商店页面,搜索“冰球大莽斗”即可找到该游戏。配置介绍根据官方......
  • 前端【Vant】01-移动端组件库
    1、介绍Vant是一个轻量、可靠的移动端组件库,于2017年开源。目前Vant官方提供了 Vue2版本、Vue3版本和微信小程序版本,并由社区团队维护 React版本和支付宝小程序版本。2、安装1#Vue3项目,安装最新版Vant:2npmivant-S34#Vue2项目,安装Vant......
  • 前端【VUE】09-vue【Eslint】
    一、ESLint在vscode插件中搜索ESLint,https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint 什么是ESLint官方概念:ESLint是可组装的JavaScript和JSX检查工具。通俗理解:一个工具,用来约束团队成员的代码风格。当通过@vue......
  • 前端【VUE】08-vue【插槽】【默认插槽】【具名插槽】【作用域插槽】
    1、默认插槽默认插槽对应的插槽的名字为default  1、组件目录下components/MyDialog.vue1<template>2<divclass="dialog">3<divclass="dialog-header">4<h3>友情提示</h3>5<spanclass="close......