blob: 43742f45c07cdfc060b3b5dca380e49e23720e19 [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001module.exports = {
2 data() {
3 return {}
4 },
5 onLoad() {
6 // getRect挂载到$u上,因为这方法需要使用in(this),所以无法把它独立成一个单独的文件导出
7 this.$u.getRect = this.$uGetRect
8 },
9 methods: {
10 // 查询节点信息
11 // 目前此方法在支付宝小程序中无法获取组件跟接点的尺寸,为支付宝的bug(2020-07-21)
12 // 解决办法为在组件根部再套一个没有任何作用的view元素
13 $uGetRect(selector, all) {
14 return new Promise(resolve => {
15 uni.createSelectorQuery().
16 in(this)[all ? 'selectAll' : 'select'](selector)
17 .boundingClientRect(rect => {
18 if (all && Array.isArray(rect) && rect.length) {
19 resolve(rect)
20 }
21 if (!all && rect) {
22 resolve(rect)
23 }
24 })
25 .exec()
26 })
27 },
28 getParentData(parentName = '') {
29 // 避免在created中去定义parent变量
30 if(!this.parent) this.parent = false;
31 // 这里的本质原理是,通过获取父组件实例(也即u-radio-group的this)
32 // 将父组件this中对应的参数,赋值给本组件(u-radio的this)的parentData对象中对应的属性
33 // 之所以需要这么做,是因为所有端中,头条小程序不支持通过this.parent.xxx去监听父组件参数的变化
34 this.parent = this.$u.$parent.call(this, parentName);
35 if(this.parent) {
36 // 历遍parentData中的属性,将parent中的同名属性赋值给parentData
37 Object.keys(this.parentData).map(key => {
38 this.parentData[key] = this.parent[key];
39 });
40 }
41 },
42 // 阻止事件冒泡
43 preventEvent(e) {
44 e && e.stopPropagation && e.stopPropagation()
45 }
46 },
47 onReachBottom() {
48 uni.$emit('uOnReachBottom')
49 }
50}