mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-28 04:16:35 +08:00
57 lines
990 B
JavaScript
57 lines
990 B
JavaScript
export function ChildrenMixin(parent) {
|
|
return {
|
|
inject: [parent],
|
|
|
|
computed: {
|
|
parent() {
|
|
return this[parent];
|
|
},
|
|
|
|
index() {
|
|
this.bindRelation();
|
|
return this.parent.children.indexOf(this);
|
|
}
|
|
},
|
|
|
|
created() {
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
export function ParentMixin(parent) {
|
|
return {
|
|
provide() {
|
|
return {
|
|
[parent]: this
|
|
};
|
|
},
|
|
|
|
data() {
|
|
return {
|
|
children: []
|
|
};
|
|
}
|
|
};
|
|
}
|