运行环境node10.xvue.js2.6.xvue-cli3.x组件化是vue的核心思想,它可以提高开发效率,方便复用,简化调试步骤,提高整个项目的可维护性,方便多人开发组件通信parentcomponent=>childcomponent:+1)attributeprops//childprops:{msg:String}//parent+2)feature$attrs//child:foo没有在props中声明
{{$attrs.foo}}
//parent
+3)referencerefs//parent
mounted(){this.$refs.hw.xx='xxx'}+4)子元素$children(不保证子元素的顺序)//parentthis.$children[0].xx='xxx'childcomponent=>parentcomponent:customevent//childthis.$emit('add',good)//parent
兄弟组件:通过共同的祖先组件$parent或$root进行桥接。//brother1this.$parent.$on('foo',handle)//brother2this.$parent.$emit('foo')由于嵌套层太多,在祖先和后代之间传递props是不切实际的。Vue提供了provide/injectAPI来完成这个任务1.provide/inject:Abilitytopassvaluesfromancestorstodescendants//ancestorprovide(){return{foo:'foo'}}//descendantinject:['foo']任意两个Between组件:eventbus或者vuex+。事件总线:创建负责事件派发、监听和回调管理的Bus类//Bus:事件派发、监听和回调管理类Bus{constructor(){this.callbacks={}}$on(name,fn){this.callbacks[名称]=this.callbacks[名称]||[]在实践中,可以使用Vue代替Bus,因为它已经实现了相应的功能vuex:创建一个唯一的全局数据管理器存储,用它来管理数据和通知状态变化的组件。示例:组件通信组件通信示例代码,请参考components/communicateslot。槽语法是Vue实现的一种内容分发API,用于复合组件开发。该技术广泛应用于通用组件库的开发。AnonymousslotNamedslot分发内容到子组件指定位置//comp1
//parent
hellothis.callbacks[name].push(fn)}$emit(name,args){if(this.callbacks[name]){this.callbacks[name].forEach(cb=>cb(args))}}//main.jsVue.prototype.$bus=newBus()//child1this.$bus.$on('foo',handle)//child2this.$bus.$emit('foo')+.在实践中,可以使用Vue代替Bus,因为它已经实现了相应的功能+.vuex:创建一个唯一的全局数据管理器存储,通过它管理数据并通知组件状态变化示例:组件通信组件通信示例代码请参考tocomponents/communicateslotslot语法是VueDistributionAPI实现的用于复合组件开发的内容。该技术广泛应用于通用组件库的开发。1.Anonymousslot//comp1
//parent
hello2.Namedslot将内容分发到子组件的指定位置//comp2
//parent
NamedslotContent...3.Scopeslot分发内容需要用到子组件中的数据//comp3
//parent
来自子组件数据:{{slotProps.foo}}4.例如slot相关示例请参考components/slots中的代码