mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-06 03:58:05 +08:00
fix(mixin): fix scroll function are not recycled (#5086)
* fix(mixins): fix scroll function are not recycled * fix(mixins): opt page scroll mixin recycle
This commit is contained in:
parent
66c967b782
commit
46b8d053e3
@ -1,3 +1,4 @@
|
|||||||
|
import { isFunction } from '../common/validator';
|
||||||
import { getCurrentPage, isDef } from '../common/utils';
|
import { getCurrentPage, isDef } from '../common/utils';
|
||||||
|
|
||||||
type IPageScrollOption = WechatMiniprogram.Page.IPageScrollOption;
|
type IPageScrollOption = WechatMiniprogram.Page.IPageScrollOption;
|
||||||
@ -19,32 +20,48 @@ function onPageScroll(event?: IPageScrollOption) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export const pageScrollMixin = (scroller: Scroller) =>
|
|
||||||
Behavior({
|
export function pageScrollMixin(scroller: Scroller) {
|
||||||
attached() {
|
return Behavior({
|
||||||
|
attached(this: WechatMiniprogram.Component.TrivialInstance) {
|
||||||
const page = getCurrentPage<{ vanPageScroller: Scroller[] }>();
|
const page = getCurrentPage<{ vanPageScroller: Scroller[] }>();
|
||||||
|
|
||||||
if (!isDef(page)) {
|
if (!isDef(page)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(page.vanPageScroller)) {
|
const _scroller = scroller.bind(this);
|
||||||
page.vanPageScroller.push(scroller.bind(this));
|
|
||||||
} else {
|
const { vanPageScroller = [] } = page;
|
||||||
page.vanPageScroller =
|
|
||||||
typeof page.onPageScroll === 'function'
|
if (!vanPageScroller.length && isFunction(page.onPageScroll)) {
|
||||||
? [page.onPageScroll.bind(page), scroller.bind(this)]
|
vanPageScroller.push(page.onPageScroll.bind(page));
|
||||||
: [scroller.bind(this)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vanPageScroller.push(_scroller);
|
||||||
|
|
||||||
|
page.vanPageScroller = vanPageScroller;
|
||||||
page.onPageScroll = onPageScroll;
|
page.onPageScroll = onPageScroll;
|
||||||
|
|
||||||
|
this._scroller = _scroller;
|
||||||
},
|
},
|
||||||
|
|
||||||
detached() {
|
detached(this: WechatMiniprogram.Component.TrivialInstance) {
|
||||||
const page = getCurrentPage<{ vanPageScroller: Scroller[] }>();
|
const page = getCurrentPage<{ vanPageScroller: Scroller[] }>();
|
||||||
if (isDef(page)) {
|
|
||||||
page.vanPageScroller =
|
if (!isDef(page) || !isDef(page.vanPageScroller)) {
|
||||||
page.vanPageScroller?.filter((item) => item !== scroller) || [];
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { vanPageScroller } = page;
|
||||||
|
|
||||||
|
const index = vanPageScroller.findIndex(v => v === this._scroller);
|
||||||
|
|
||||||
|
if (index > -1) {
|
||||||
|
page.vanPageScroller.splice(index, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._scroller = undefined;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user