首页 > 其他 > 详细

[Vue]对Vue,Vuex.store,mixins理解

时间:2021-04-20 13:51:14      阅读:37      评论:0      收藏:0      [点我收藏+]

== vue ==

this.$dispatch、this.$broadcast弃用,改用this.$emit、this.$on

this.$emit:子组件调用父组件传给子组件的方法
this.$on/this.$once:创建一个事件监听,可用this.$emit触发
this.$off:删除一个事件监听

this.$set:新增/修改嵌套对象属性,可触发视图更新

 

 

== vuex.store ==
PS:当作全局变量
PS:里面的方法都是对state的变量进行修改

state:存放变量数据
-> this.$store.state.变量名
getters:返回计算结果,相当于C#的GET/SET方法中的GET
-> this.$store.getters.变量名
mutations:同步方法,相当于C#的GET/SET方法中的SET
-> this.$store.commit(‘方法名‘, 参数...);
actions:对mutations方法进行异步操作,建议后缀加上Asyn
-> this.$store.dispatch(‘方法名‘, 参数...);


mapState/mapGetters/mapMutations/mapActions:映射state/getters/mutations/actions到this
-> import { mapState, mapGetters,mapMutations, mapActions } from ‘vuex‘;
-> computed: {
-> ...mapState([‘变量名‘...]), // 不用在data声明,即可在本页内<template><script>使用
-> ...mapGetters([‘变量名‘...]), // 同上
-> },
-> methods: {
-> ...mapMutations([‘方法名‘...]), // 可在本页内调用,this.方法名(参数...);
-> ...mapActions([‘方法名‘...]), // 同上
-> },

 

== mixins ==
PS:内部方法可以被页面方法重写,参考:https://www.jianshu.com/p/bcff647d24ec
PS:官方建议添加前缀$_,防止污染

-> import 混合对象 from ‘混合对象路径‘;
-> mixins: [混合对象...], // 此处会调用内部生命周期
// 映射到this,可在本页内调用,this.混合对象内部方法名(参数...);

[Vue]对Vue,Vuex.store,mixins理解

原文:https://www.cnblogs.com/hcbin/p/14680476.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!