1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import { ValueComponent, Component, Watch, Prop, Ref } from 'keepeact';
@Component() export default class Button extends ValueComponent { count = 0; showStop = true; @Prop() defaultCount: number; @Prop() max: number = 3; @Ref('button') buttonEl: HTMLElement; @Watch('count', { immediate: true, }) countChange(a, b) { console.log('监测 count:', `oldValue: ${a}`, `newValue: ${b}`); this.showStop = this.count > this.max; }
get countValue() { return this.showStop ? 'stop' : this.count; }
button() { this.$nextTick(() => { console.log(this.buttonEl.innerText); }); return ( <button onClick={() => { if (this.showStop) return; this.count++; this.onChange(this.count); }} > click+1 </button> ); } render() { return ( <div id="wuxunyu" ref="button"> {this.countValue} {this.button()} </div> ); } }
|