import C from 'bsl-mp/app/Component'; import * as Export from './export'; const computedBehavior = require('miniprogram-computed').behavior; const state = { _height: 0, _animation: {} as WechatMiniprogram.Animation, animationData: {} }; class Instance extends C { constructor() { super(state, { open: Boolean }); } public behaviors = [computedBehavior]; public externalClasses = ['wrap-cls']; public options = { // 指定所有 _ 开头的数据字段为纯数据字段 pureDataPattern: /^_/ }; public watch = { 'open'() { const self = this as any as Instance; if (self.properties.open && self.data._animation.height) { self.data._animation.height(self.data._height).step(); self.setData({ animationData: self.data._animation.export() }); } } } public ready() { const detail: Export.OnReady = { instanceRef: this }; this.setData({ _animation: wx.createAnimation({ duration: 500, timingFunction: 'ease', }) }); // 初始化展开 if (this.properties.open) { this.open(() => { const animation = wx.createAnimation({ duration: 0, }); animation.height(this.data._height).step(); this.setData({ animationData: animation.export() }); }); } this.triggerEvent('ready', detail); } public open(callback: () => void) { this.createSelectorQuery().select('.slot').fields({ size: true }, (res) => { this.setData({ _height: res.height }); callback(); }).exec(); } public close(callback: () => void) { this.data._animation.height(0).step(); this.setData({ animationData: this.data._animation.export() }); callback(); } } Component(new Instance().init());