vue 通过下拉框组件学习vue中的父子通讯

如果说vue组件化开发中第一步应该了解的是什么的话,那无疑是父子组件之间是如何实现通讯的(说白了就是父子组件中数据是如何传递的),只有理解了这一步,才能更好的开发组件

这里先提出两个关键词: props 与 emit :

写这个组件之前,先看看效果图:

组件开发分析:

既然是组件:

  • 首先组件内部数据内容肯定是可变的(如上图中的"按时间排序"之类的),这必须由父组件传入(即父组件如何将数据传个父组件);
  • 在选择了内容之后,如何将数据传出来(即子组件如何将数据传给父组件)

先写结构:

父组件

rush:js;">

子组件

rush:js;">

总结

  • 从以上的示例可以看出来,父组件传入数据,需要在父组件中线绑定一个属性,挂载需要传入的数据;
  • 子组件接收父组件的数据通过 props 方法来接收;
  • 子组件传递数据需要使用 emit 方法来绑定父组件中事先设定好的方法,从而动态传递操作后需要的数据

最终效果如下:

附上组件中的css,仅供参考:

rush:js;"> .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中的父子通讯。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持

相关文章

可以通过min-width属性来设置el-table-column的最小宽度。以...
yarn dev,当文件变动后,会自动重启。 yanr start不会自动重...
ref 用于创建一个对值的响应式引用。这个值可以是原始值(如...
通过修改 getWK005 函数来实现这一点。这里的 query 参数就是...
<el-form-item label="入库类型" ...
API 变动 样式类名变化: 一些组件的样式类名有所变动,可能需...