当前位置: 首页 > news >正文

Vue3 Vuex状态管理多组件传递数据简单应用

去官网学习→安装 | Vuex

cd 项目 安装 Vuex: npm install --save vuex

或着 创建项目时勾选Vuex         vue create vue-demo

? Please pick a preset: Manually select features
? Check the features needed for your project: (Press <space> to select, <a> to toggle all, <i> to invert
selection, and <enter> to proceed)
 (*) Babel
 ( ) TypeScript
 (*) Progressive Web App (PWA) Support
 (*) Router
>(*) Vuex
 ( ) CSS Pre-processors
 ( ) Linter / Formatter
 ( ) Unit Testing
 ( ) E2E Testing

运行示例:

 

 Vuex代码:store/index.js

// 引用 Vuex
import { createStore } from 'vuex'
// 引用 axios 网络请求 安装 cnpm install --save axios
import axios from 'axios';//导出      在main.js 引用
export default createStore({//状态管理  通用的数据    数据改变时,组件中引用到此数据的内容都会发生改变state: {name:"张三丰",age: 198},//获取state中的数据  进行数据校验getters: {//自定义个方法 ,进行数据校验getAge(state){return state.age < 200 ? state.age: "张三丰只能活到200岁";}},//用于 更改state中的数据mutations: {//自定义个方法 ,进行数据修改 自定义参数numbaddAge(state,numb){state.age +=numb;}},//Action 提交的是 mutation,而不是直接变更状态。//Action 可以包含任意异步操作。actions: {//自定义个方法 ,获取网络数据 进行数据修改 自定义参数numbasyncAddage({commit}){//http://www.csdnts.com/getTestData.jspx  域名是假的,后台接口已经处理了跨域问题//接口数据:[{"name":"张三丰","sex":"男","age":25},{"name":"周芷若","sex":"女","age":22}]axios.get("http://www.csdnts.com/getTestData.jspx").then(res =>{console.log(res.data);//调用 mutations中 addAge方法  每次+25commit("addAge",(res.data[0]).age)}).catch(err =>{console.log(err);})}},//模块化管理vuex 允许我们将 store 分割成模块(module)。//每个模块拥有自己的 state、mutation、action、getter、//甚至是嵌套子模块——从上至下进行同样方式的分割modules: {}
})

代码:HelloWorld.vue

<template><div class="hello"><h1>{{ msg }}</h1><!-- $store.state --><p>方式一 state中的name->{{$store.state.name}}</p><p>方式一 state中的age->{{$store.state.age}}</p><!-- $store.getters --><p>方式一 getters中的getAge()->{{$store.getters.getAge}}</p><hr><p>方式二 state中的name->{{name}}</p><p>方式二 state中的age->{{age}}</p><p>方式二 getters中的getAge()->{{getAge}}</p><button @click="onClickaddAge">更改年龄数据</button><button @click="asynconClickaddAge">获取网络数据更改年龄</button></div>
</template><script>
//方式二 import  mapState
import { mapState } from 'vuex';
//方式二 import  mapGetters
import { mapGetters } from 'vuex';// import  mapMutations
import { mapMutations } from 'vuex';
//  import  mapActions
import { mapActions } from 'vuex';export default {name: 'HelloWorld',props: {msg: String},//computed  computed:{//State 简化写法...mapState(["name","age"]),//getters ...mapGetters(["getAge"]),},methods:{//mutations 简化写法...mapMutations(["addAge"]),onClickaddAge(){//this.$store.commit 调用getAge 参数 20// this.$store.commit("addAge",20)this.addAge(1);},//actions...mapActions(["asyncAddage"]),asynconClickaddAge(){// this.$store.dispatch()  调用 asyncAddage()//this.$store.dispatch("asyncAddage")this.asyncAddage();}}}
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {margin: 40px 0 0;
}
ul {list-style-type: none;padding: 0;
}
li {display: inline-block;margin: 0 10px;
}
a {color: #42b983;
}
</style>

代码:main.js

import { createApp } from 'vue'
import App from './App.vue'
import './registerServiceWorker'
//router
import router from './router'
//vuex
import store from './store'//.use(store)
createApp(App).use(store).use(router).mount('#app')

代码:AboutView.vue

<template><div class="about"><h1>关于-页面</h1><!-- $store.state    直接引用  --><p>方式一 state中的name->{{ $store.state.name }}</p><p>方式一 state中的age->{{ $store.state.age }}</p><!-- $store.getters --><p>方式一 getters中的getAge()->{{$store.getters.getAge}}</p><hr><p>方式二 state中的name->{{name}}</p><p>方式二 state中的age->{{age}}</p></div>
</template><script>
//方式二 imper  mapState
import { mapState } from 'vuex';export default {name: 'AboutView',//computed   获取State 中数据computed:{...mapState(["name","age"])}
}
</script>

http://www.lryc.cn/news/127574.html

相关文章:

  • Beats:安装及配置 Metricbeat (一)- 8.x
  • openCV使用c#操作摄像头
  • Centos 防火墙命令
  • 【第二讲---初识SLAM】
  • C++ 面向对象三大特性——继承
  • LC-相同的树
  • RocketMQ部署 Linux方式和Docker方式
  • css内容达到最底部但滚动条没有滚动到底部
  • 机器学习深度学习——transformer(机器翻译的再实现)
  • 神经网络基础-神经网络补充概念-30-搭建神经网络块
  • 在线吉他调音
  • Windows11 Docker Desktop 启动 -wsl kernel version too low
  • Golang 中的 unsafe 包详解
  • linux 的swap、swappiness及kswapd原理【转+自己理解】
  • 什么是Java中的适配器模式?
  • MYSQL线上无锁添加索引
  • 如何实现客户自助服务?打造产品知识库
  • LeetCode环形子数组的最大和(编号918)
  • PhpOffice/PhpSpreadsheet读取和写入Excel
  • jenkins自动化部署Jenkinsfile文件配置
  • 【socket编程简述】TCP UDP 通信总结、TCP连接的三次握手、TCP断开的四次挥手
  • 多线程-死锁
  • P1006 [NOIP2008 提高组] 传纸条
  • 杭电比赛总结
  • dom靶场
  • go struct 的常见问题
  • Linux系统下的性能分析命令
  • 第十三课:QtCmd 命令行终端应用程序开发
  • Jmeter进阶使用:BeanShell实现接口前置和后置操作
  • 【知识分享】高防服务器的防御机制