首页
统计
关于
Search
1
Sealos3.0离线部署K8s集群
1,164 阅读
2
类的加载
792 阅读
3
Spring Cloud OAuth2.0
749 阅读
4
SpringBoot自动装配原理
708 阅读
5
集合不安全问题
614 阅读
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Canvas
Linux
容器
Docker
Containerd
Kubernetes
Python
FastApi
登录
Search
标签搜索
Java
CSS
mysql
RabbitMQ
JavaScript
Redis
JVM
Mybatis-Plus
Camunda
多线程
CSS3
Python
Spring Cloud
注解和反射
Activiti
工作流
SpringBoot
Mybatis
Canvas
Spring
蘇阿細
累计撰写
404
篇文章
累计收到
4
条评论
首页
栏目
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Canvas
Linux
容器
Docker
Containerd
Kubernetes
Python
FastApi
页面
统计
关于
搜索到
404
篇与
的结果
2022-07-17
Vuex
1. 概念实现集中式状态(数据)管理的一个插件,对vue应用中多个组件的共享状态进行集中式的管理(读/写),也是一种组件间通信的方式(适用于任意组件间通信)2. 搭建vuex环境安装(以vue2为例):npm i vuex@3index.jsimport Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) //用于响应组件中的动作 const actions = { } //用于操作数据 const mutations = { } //用于存储数据 const state = { } //创建并暴露store export default new Vuex.Store({ actions,mutations,state })main.jsimport Vue from 'vue' import App from './App.vue' import store from './store' //关闭生产提示 Vue.config.productionTip =false new Vue({ el: '#app', render: h => h(App), store })3. 基本使用(1)配置actions,mutations,stateimport Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) //用于响应组件中的动作 const actions = { increment(context,value) { console.log('actions中的increment被调用') context.commit('INCREMENT', value) }, decrement(context,value) { context.commit('DECREMENT', value) }, incrementOdd(context,value) { if (context.state.sum % 2) { context.commit('INCREMENTODD', value) } }, incrementWait(context,value) { setTimeout(() => { context.commit('INCREMENTWAIT', value) }, 500) } } //用于操作数据 const mutations = { INCREMENT(state,value) { console.log('mutations中的INCREMENT被调用') state.sum += value }, DECREMENT(state,value) { state.sum -= value }, INCREMENTODD(state,value) { state.sum += value }, INCREMENTWAIT(state,value) { state.sum += value } } //用于存储数据 const state = { //当前的和 sum: 0, } //创建并暴露store export default new Vuex.Store({ actions,mutations,state })(2)组件中读取vuex中的数据:$store.state.sum(3)组件中修改vuex中的数据:$store.dispatch('方法名(小写)', 数据) 或 this.$store.commit('方法名(大写)', 数据) 注:若没有网络请求或其他业务逻辑,组件中可以跳过actions(即不写dispatch),直接(调用mutations)commit4. getters的使用(1)概念:当state中的数据需要经过加工后再使用时,可以使用getters(2)在store中追加getters配置//用于加工state中的数据 const getters = { bigSum(state) { return state.sum * 10 } } //创建并暴露store export default new Vuex.Store({ actions,mutations,state,getters })(3)组件中读取数据:$store.gettes.bigSum5. 四个map方法的使用(1)mapState(映射state中的属性为计算属性)//借助mapState生成计算属性,从state中读取数据 //对象写法 ...mapState({sum:'sum',school:'school',subject:'subject'}) //数组写法 ...mapState(['sum','school','subject']),(2)mapGetters(映射getters中的数据为计算属性)//借助mapGetters生成计算属性,从getter中读取数据 //对象写法 ...mapGetters({bigSum:'bigSum'}) //数组写法 ...mapGetters(['bigSum'])(3)mapMutations 用于帮助生成与mutations对话的方法,即:包含$store.commit(xxx)的函数//借助mapMutations生成对应的方法,方法中会调用commit去联系mutations //对象写法 ...mapMutations({increment:'INCREMENT',decrement:'DECREMENT'}), //数组写法 ...mapMutations(['INCREMENT','DECREMENT']),(4)mapActions 用于帮助生成与actions对话的方法,即:包含$store.dispatch(xxx)的函数//借助mapActions生成对应的方法,方法中会调用dispatch去联系actions //对象写法 ...mapActions({incrementOdd:'incrementOdd',incrementWait:'incrementWait'}), //数组写法 ...mapActions(['incrementOdd','incrementWait'])注:使用mapMutations与mapActions 时,若需要传递参数,需要在模板中绑定事件时就传递,否则默认的传参为时间对象6. 模块化、命名空间(1)目的:便于代码维护,数据分类更加明确(2)修改store/index.jsindex.jsimport Vue from 'vue' import Vuex from 'vuex' //求和相关 import countOptions from './count' //人员管理相关 import personOptions from './person' Vue.use(Vuex) //创建并暴露store export default new Vuex.Store({ modules: { countOptions, personOptions } })count.jsexport default { //开启命名空间 namespaced: true, actions: { ...... }, mutations: { ...... }, state: { ...... }, getters: { ...... }, }person.jsimport axios from "axios"; import {nanoid} from "nanoid"; export default { namespaced: true, actions: { ...... }, mutations: { ...... }, state: { ...... }, getters: { ...... }, }(3)开启命名空间后:组件读取state数据//方式一:直接读取 this.$store.state.personOptions.personList //方式二:借助map方法 ...mapState('personOptions', ['personList'])组件读取getters数据//方式一:直接读取 this.$store.getters['personOptions/firstPersonName'] //方式二:借助map方法 ...mapGetters('countOptions', ['bigSum'])组件调用dispatch方法//方式一:直接调用 this.$store.dispatch('personOptions/addPerson', personObj) //方式二:借助map方法 //参数在模板中绑定事件时就传递 ...mapActions('countOptions',['incrementOdd','incrementWait'])组件调用commit方法//方式一:直接调用 this.$store.commit('personOptions/ADD_PERSON', personObj) //方式二:借助map方法 //参数在模板中绑定事件时就传递 ...mapMutations('countOptions',{increment:'INCREMENT',decrement:'DECREMENT'}),
2022年07月17日
67 阅读
0 评论
0 点赞
2022-07-17
插槽
定义:让父组件可以向子组件指定位置插入html结构,也是一种组件间通信方式(父 ---> 子)分类:默认、具名、作用域使用方式:(1)默认插槽<!-- 父组件 --> <Category> <div> html结构 </div> </Category> <!-- 子组件 --> <template> <div class="category"> <!-- 定义一个插槽,当使用者没有传递具体结构时,使用默认值填充 --> <slot>默认插槽</slot> </div> </template>(2)具名插槽<!-- 父组件 --> <Category> <template v-slot:center> <div> html结构1 </div> </template> <template v-slot:footer> <div> html结构2 </div> </template> </Category> <!-- 子组件 --> <template> <div> <!-- 定义一个插槽,当使用者没有传递具体结构时,使用默认值填充 --> <slot name="center">默认插槽</slot> <slot name="footer">默认插槽</slot> </div> </template>(3)作用域插槽 a. 定义:数据在组件的自身,但数据生成的结构由组件的使用者决定(list数据在Category 组件中,但使用数据遍历出来的结构由App组件决定) b. 使用:<!-- 父组件 --> <Category> <template slot-scope="{games}"> <ul> <!-- 无序列表 --> <li v-for="(item,index) in games" :key="index">{{ item }}</li> </ul> </template> </Category> <Category> <template slot-scope="{games}"> <ol> <!-- 有序列表 --> <li v-for="(item,index) in games" :key="index">{{ item }}</li> </ol> </template> </Category> <!-- 子组件 --> <template> <div> <!-- 定义一个插槽,当使用者没有传递具体结构时,使用默认值填充 --> <slot :games="games">默认插槽</slot> </div> </template> <script> export default { name: "MyCategory", // 数据在组件自身 data() { return { games: ['魔兽争霸', 'QQ飞车', 'Dota自走棋', '地下城与勇士'] } } } </script>注:作用域插槽也可以添加name属性,使用时同理具名插槽
2022年07月17日
47 阅读
0 评论
0 点赞
2022-07-17
配置代理
方式一在vue.config.js中添加如下配置:devServer: { proxy: 'http://localhost:5000' }注:(1)该方式配置简单,但不能配置多个代理,且不能灵活的控制请求是否走代理 (2)当请求了前端不存在的资源时,那么该请求会转发给服务器(优先匹配前端资源)方式二devServer: { proxy: { '/api': { target: 'http://localhost:5000', //路径重写 pathRewrite: {'^/api':''}, ws: true, //用于空值请求头中的host值(默认值为true) changeOrigin: true }, '/demo': { target: 'http://localhost:5001', pathRewrite: {'^/demo':''}, ws: true, changeOrigin: true } } }注:可以配置多个代理和灵活的控制请求是否走代理(请求时必须加上前缀)
2022年07月17日
33 阅读
0 评论
0 点赞
2022-07-17
过度与动画
作用:再插入、更新或移除DOM元素时,在合适的时候给元素添加样式名语法:(1)准备样式元素进入的样式v-enter:进入的起点v-enter-active:进入过程中v-enter-to:进入的终点元素离开的样式v-leave:离开的起点v-leave-active:离开过程中v-leave-to:离开的终点(2)使用<transition>包裹要过度的元素,并配置name属性<transition name="demo"> <h1 v-show="isShow"> 孙笑川 </h1> </transition>(3)注:若有多个元素需要过度,则需要使用:<transition-group>,且每个元素都需要指定key值
2022年07月17日
47 阅读
0 评论
0 点赞
2022-07-17
nextTick
语法:this.$nextTick(回调函数)作用:在下一次DOM更新结束后执行指定的回调什么时候用?当数据改变后,需要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行
2022年07月17日
28 阅读
0 评论
0 点赞
2022-07-17
消息订阅与发布(pubsub)
定义:一种组件间的通信方式,适用于任意组件间通信引入:npm i pubsub-js接收数据:methods() { demo(msgName,data) { ...... } } mounted() { //订阅消息 this.pubId = pubsub.subscribe('xxx', this.demo) }提供数据:pubsub.publish('xxx', data)注:最好在beforeDestroy钩子函数中,使用pubsub.unsubscribe(this.pubId)取消订阅
2022年07月17日
23 阅读
0 评论
0 点赞
2022-07-17
全局事件总线(GlobalEventBus)
定义:一种组件间的通信方式,适用于任意组件间通信安装全局事件总线new Vue({ ...... beforeCreate() { //安装全局事件总线 Vue.prototype.$bus = this } })使用:(1)接收数据:A组件想接收数据,则在A组件中给$bus绑定自定义事件,且回调留在A组件自身methods: { demo(data) { ...... } } mounted() { this.$bus.$on('xxx', this.demo) }(2)提供数据:this.$bus.$emit('xxx', data)注:最好在beforeDestroy钩子函数中,使用$off('xxx')解绑当前组件所用到的自定义事件
2022年07月17日
44 阅读
0 评论
0 点赞
2022-07-17
组件的自定义事件
定义:一种组件间的通信方式,适用于:子传父使用场景:A是父组件,B是子组件,B想给A传数据,那么就要在A中给B绑定自定义事件(事件的回调在A中)绑定自定义事件:(1)方式一:<Demo @getName="getName"/> 或 <Demo v-on:getName="getName"/>(2)方式二:<Demo ref="getName"/> mounted() { //this.xxx为回调方法 this.$refs.getName.$on('getName', this.xxx) }(3)若想让自定义事件只触发一次,可以使用once修饰符或$once方法触发自定义事件:this.$emit('getName', 参数)解绑自定义事件:解绑一个:this.$off('getName')解绑多个:this.$off(['getName', 'getAge'])组件上可以使用native修饰符绑定原生DOM事件注:通过this.$refs.getName.$on('getName', this.xxx)绑定自定义事件时,回调要么配置在methods中,要么用箭头函数
2022年07月17日
47 阅读
0 评论
0 点赞
1
...
17
18
19
...
51