1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- export function ChildrenMixin(parent, options = {}) {
- const indexKey = options.indexKey || 'index';
- return {
- inject: {
- [parent]: {
- default: null,
- },
- },
- mounted() {
- this.parent = this[parent];
- this.bindRelation();
- },
- // #ifdef VUE2
- beforeDestroy() {
- if (this.parent) {
- this.parent.children = this.parent.children.filter(
- (item) => item !== this
- );
- uni.$emit("childrenReady" + this.parent._uid, this);
- }
- },
- // #endif
- // #ifdef VUE3
- beforeUnmount() {
- if (this.parent) {
- this.parent.children = this.parent.children.filter(
- (item) => item !== this
- );
- uni.$emit("childrenReady" + this.parent._uid, this);
- }
- },
- // #endif
- methods: {
- bindRelation() {
- if (!this.parent || this.parent.children.indexOf(this) !== -1) {
- return;
- }
- const children = [...this.parent.children, this];
- this.parent.children = children;
- this.index = this.parent.children.indexOf(this);
- uni.$emit("childrenReady" + this.parent._uid, this);
- },
- },
- };
- }
- export function ParentMixin(parent) {
- return {
- provide() {
- return {
- [parent]: this,
- };
- },
- created() {
- this.children = [];
- },
- // #ifdef VUE2
- beforeDestroy() {
- uni.$off("childrenReady" + this._uid)
- },
- // #endif
- // #ifdef VUE3
- beforeUnmount() {
- uni.$off("childrenReady" + this._uid)
- },
- // #endif
- };
- }
|