如果说vue组件化开发中第一步应该了解的是什么的话,那无疑是父子组件之间是如何实现通讯的(说白了就是父子组件中数据是如何传递的),只有理解了这一步,才能更好的开发组件
这里先提出两个关键词: props 与 emit :
写这个组件之前,先看看效果图:
组件开发分析:
既然是组件:
先写结构:
父组件
子组件
{{ selectData.selectOptions[selectData.defaultIndex].context }}
- {{ selectData.selectOptions[index].context }}
总结
最终效果如下:
附上组件中的css,仅供参考:
.select-box{ position: relative; max-width: 250px; line-height: 35px; margin: 50px auto; } .select-title{ position: relative; padding: 0 30px 0 10px; border: 1px solid #d8dce5; border-radius: 5px; transition-duration: 300ms; cursor: pointer; } .select-title:after{ content: ''; position: absolute; height: 0; width: 0; border-top: 6px solid #d8dce5; border-left: 6px solid transparent; border-right: 6px solid transparent; right: 9px; top: 0; bottom: 0; margin: auto; transition-duration: 300ms; transition-timing-function: ease-in-out; } .select-title-active{ border-color: #409eff; } .select-title-active:after{ transform: rotate(-180deg); border-top-color: #409eff; } .select-options{ position: absolute; padding:10px 0; top: 45px; border:1px solid #d8dce5; width: 100%; border-radius: 5px; } .select-option-item{ padding:0 10px; cursor: pointer; transition-duration: 300ms; } .select-option-item:hover,.select-option-active{ background: #f1f1f1; color: #409eff; } .arrow-top{ position: absolute; height: 0; width: 0; top: -7px; border-bottom: 7px solid #d8dce5; border-left: 7px solid transparent; border-right: 7px solid transparent; left: 0; right: 0; margin: auto; z-index: 99; } .arrow-top:after{ content: ''; position: absolute; display: block; height: 0; width: 0; border-bottom: 6px solid #fff; border-left: 6px solid transparent; border-right: 6px solid transparent; left: -6px; top: 1px; z-index: 99; } .slide-down-enter-active,.slide-down-leave{ transition: all .3s ease-in-out; transform-origin:0 top; transform: scaleY(1); } .slide-down-enter{ transform: scaleY(0); } .slide-down-leave-active{ transition: all .3s ease-in-out; transform-origin:0 top; transform: scaleY(0); }
总结
以上所述是小编给大家介绍的vue 通过下拉框组件学习vue中的父子通讯,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!