内置弹层类组件基本都是基于此组件实现,包括:、Picker、、DatePicker、、SegmentPicker、、ActionSheet。
注: 以上组件都是基于 create-api 实现,所以在使用之前,请确保自己了解过 。
组件
基本用法
methods: {
showPopup(refId) {
const component = this.- refs[refId]
component.show()
setTimeout(() => {
component.hide()
}
}
}
组件默认是隐藏的,需要调用组件实例的 show() 方法才会显示,调用组件实例的 hide() 方法隐藏,这里的 showPopup() 就是做的这件事情(首先显示,1 秒钟后隐藏)
不要背景层
设置 mask 为 false,即不显示背景层
显示内容 HTML
<cube-popup type="my-popup" :mask="false" content="<i>My Popup Content 3</i>" ref="myPopup3">
<cube-button @click="showPopup('myPopup3')">
Show Popup - with content
只需要传入 content,内容是一段 HTML 片段
控制位置&蒙层点击隐藏1.9.6
const positions = ['top', 'right', 'bottom', 'left', 'center']
let cur = 0
export default {
data() {
return {
position: ''
}
},
methods: {
this.position = positions[cur++]
if (cur === positions.length) {
cur = 0
const component = this.- refs.myPopup4
component.show()
setTimeout(() => {
component.hide()
}, 2000)
}
}
}
上层封装
这样就实现了一个上层封装的 CubeExtendPopup 组件,自带了一些样式,支持传入内容还有默认插槽,点击内容可隐藏。你可以这样使用(需要全局注册或者局部注册到使用的组件中):
<cube-extend-popup content="click here hide" ref="extendPopup"></cube-extend-popup>
<cube-button @click="- refs.extendPopup.show()">
Show Extend Popup
</cube-button>
原文: