index.js
1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 商品card
Component({
externalClasses: ['custom-class', 'active-class', 'sidebar-active-custom'],
relations: {
'../sidebar/index': {
type: 'parent',
linked: function () {
// 每次被插入到 custom-ul 时执行,target是 custom-ul 节点实例对象,触发在 attached 生命周期之后
},
linkChanged: function () {
// 每次被移动后执行,target是 custom-ul 节点实例对象,触发在 moved 生命周期之后
},
unlinked: function () {
// 每次被移除时执行,target是 custom-ul 节点实例对象,触发在 detached 生命周期之后
}
}
},
/**
* 组件的属性列表
*/
properties: {
title: {
type: String,
value: ''
},
},
/**
* 组件的初始数据
*/
data: {
selected: ''
},
methods: {
// 设置选中
setActive: function (selected) {
return this.setData({selected: selected});
},
// 点击事件
onClick() {
const _this = this;
const parent = this.getParents();
if (!parent || this.data.disabled) {
return;
}
const index = parent.data.children.indexOf(this);
parent.setActive(index)
_this.triggerEvent('click', index);
parent.triggerEvent('change', index);
},
// 获取父级元素
getParents() {
const nodes = this.getRelationNodes('../sidebar/index')
const node = nodes[0]
return node;
}
}
})