Sanakey
2021-01-31 53cc6f817625897935fb10c3845ad7be400f3036
提交 | 用户 | age
d98d05 1 import { VantComponent } from '../common/component';
53cc6f 2 import { useChildren } from '../common/relation';
d98d05 3 VantComponent({
53cc6f 4   relation: useChildren('sidebar-item', function () {
S 5     this.setActive(this.data.activeKey);
6   }),
d98d05 7   props: {
S 8     activeKey: {
9       type: Number,
10       value: 0,
11       observer: 'setActive',
12     },
13   },
14   beforeCreate() {
15     this.currentActive = -1;
16   },
17   methods: {
18     setActive(activeKey) {
19       const { children, currentActive } = this;
20       if (!children.length) {
21         return Promise.resolve();
22       }
23       this.currentActive = activeKey;
24       const stack = [];
25       if (currentActive !== activeKey && children[currentActive]) {
26         stack.push(children[currentActive].setActive(false));
27       }
28       if (children[activeKey]) {
29         stack.push(children[activeKey].setActive(true));
30       }
31       return Promise.all(stack);
32     },
33   },
34 });