[improvement] Collapse: use relation mixin

This commit is contained in:
陈嘉涵
2019-05-07 09:57:43 +08:00
parent 7eb5846d62
commit 912d691f9e
3 changed files with 24 additions and 32 deletions

View File

@@ -8,23 +8,33 @@ export function ChildrenMixin(parent) {
},
index() {
this.bindRelation();
return this.parent.children.indexOf(this);
}
},
created() {
const { children } = this.parent;
const index = this.parent.slots().indexOf(this.$vnode);
if (index === -1) {
children.push(this);
} else {
children.splice(index, 0, this);
}
this.bindRelation();
},
beforeDestroy() {
this.parent.children = this.parent.children.filter(item => item !== this);
},
methods: {
bindRelation() {
const { children } = this.parent;
if (children.indexOf(this) === -1) {
const vnodeIndex = this.parent.slots().indexOf(this.$vnode);
if (vnodeIndex === -1) {
children.push(this);
} else {
children.splice(vnodeIndex, 0, this);
}
}
}
}
};
}