perf(tab): set container once after mounted (#3875)

This commit is contained in:
rex 2020-12-18 15:04:31 +08:00 committed by GitHub
parent f321e202f4
commit eaf513bf2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 18 deletions

View File

@ -1,15 +1,19 @@
import { isDef, isNumber, isPlainObject, isPromise } from './validator';
import { canIUseGroupSetData } from './version';
import { canIUseGroupSetData, canIUseNextTick } from './version';
export function range(num: number, min: number, max: number) {
return Math.min(Math.max(num, min), max);
}
export function nextTick(fn: Function) {
export function nextTick(cb: (...args: any[]) => void) {
if (canIUseNextTick()) {
wx.nextTick(cb);
} else {
setTimeout(() => {
fn();
cb();
}, 1000 / 30);
}
}
let systemInfo: WechatMiniprogram.GetSystemInfoSyncResult;
export function getSystemInfoSync() {

View File

@ -46,3 +46,7 @@ export function canIUseGroupSetData() {
const system = getSystemInfoSync();
return compareVersion(system.SDKVersion, '2.4.0') >= 0;
}
export function canIUseNextTick() {
return wx.canIUse('nextTick');
}

View File

@ -1,6 +1,12 @@
import { VantComponent } from '../common/component';
import { touch } from '../mixins/touch';
import { getAllRect, getRect, groupSetData } from '../common/utils';
import {
getAllRect,
getRect,
groupSetData,
nextTick,
requestAnimationFrame,
} from '../common/utils';
import { isDef } from '../common/validator';
type TrivialInstance = WechatMiniprogram.Component.TrivialInstance;
@ -99,10 +105,8 @@ VantComponent({
data: {
tabs: [] as Record<string, unknown>[],
lineStyle: '',
scrollLeft: 0,
scrollable: false,
trackStyle: '',
currentIndex: 0,
container: null,
skipTransition: true,
@ -110,19 +114,17 @@ VantComponent({
},
mounted() {
wx.nextTick(() => {
requestAnimationFrame(() => {
this.setData({
container: () => this.createSelectorQuery().select('.van-tabs'),
});
this.resize(true);
this.scrollIntoView();
});
},
methods: {
updateContainer() {
this.setData({
container: () => this.createSelectorQuery().select('.van-tabs'),
});
},
updateTabs() {
const { children = [], data } = this;
this.setData({
@ -158,7 +160,7 @@ VantComponent({
this.trigger('disabled', child);
} else {
this.setCurrentIndex(index);
wx.nextTick(() => {
nextTick(() => {
this.trigger('click');
});
}
@ -203,10 +205,9 @@ VantComponent({
const shouldEmitChange = data.currentIndex !== null;
this.setData({ currentIndex });
wx.nextTick(() => {
nextTick(() => {
this.resize();
this.scrollIntoView();
this.updateContainer();
this.trigger('input');
if (shouldEmitChange) {