vue-computed与watch、父子兄弟组件通信
1.computed与watch:
watch监听组件中的相关数据变动,不缓存,数据变动直接触发。注意对象值得变化需要用到deep深度监听。
computed计算属性,如:
<span>{{userInfo}}</span>
computed:{
userInfo(){
return this.userName + "-" + this.userId;
}
}
computed会缓存,只有当依赖的数据变化时,才会重新计算,不支持异步。一般用于一个属性由其他属性计算而来的情况。
2.父子兄弟组件通信:
父调用子组件,使用子组件的ref="child"调用,this.$refs.child.childMethod();
子调用父组件,父组件中在子组件上传递父方法到子组件@parentMethod="parentMethod",子组件使用emit("parentMethod")调用。
兄弟组件调用,使用$bus,如下:
兄弟1调用, this.$bus.$emit("sayToBrother2", "hello word") 兄弟2方法 //绑定方法 mounted () { this.$bus.$on("sayToBrother2", function (message) { this.message = message; }) } //销毁前,取消方法绑定,防止重复绑定 beforedestroy: function() { this.$bus.$off("sayToBrother2"); }
参考链接:https://www.cnblogs.com/jiajialove/p/11327945.html
参考链接:https://blog.csdn.net/shenxianhui1995/article/details/93010154
浙公网安备 33010602011771号