MyCounter
obsolete: not updated to new architecture
This minimal example displays a counter.
It uses a standard component and component SCSS. (For an example that uses a component template, see GreetUser.)
Usage
In any liquid file:
html<my-counterclass="MyCounter"count="12"><buttondata-ref="decrement-button"class="MyCounter__DecrementButton">-</button><spandata-ref="count-span"class="MyCount__CountSpan">12</span><buttondata-ref="increment-button"class="MyCounter__IncrementButton">+</button></my-counter>
html<my-counterclass="MyCounter"count="12"><buttondata-ref="decrement-button"class="MyCounter__DecrementButton">-</button><spandata-ref="count-span"class="MyCount__CountSpan">12</span><buttondata-ref="increment-button"class="MyCounter__IncrementButton">+</button></my-counter>
Implementation
typescript// src/components/MyCounter.tsimport {BaseComponent, Component, Prop, Ref} from 'salvo-ts';@Component()export class MyCounter extends BaseComponent {@Prop({default: 0})private _count!: number;get count() {return this._count;}set count(value: number) {this._count = value;// Every time the count changes, render the spanthis.render();}@Ref()private incrementButton!: HTMLElement;@Ref()private decrementButton!: HTMLElement;@Ref()private countSpan!: HTMLElement;ready() {// Register listenersthis.incrementButton.addEventListner('change', this.handleIncrement);this.decrementButton.addEventListner('change', this.handleDecrement);// Render initial count value - note this should _also_ be done in Liquidthis.render();}beforeDestroy() {// Make sure listeners are cleaned up after the component is destroyedthis.incrementButton.removeEventListener('change', this.handleIncrement);this.decrementButton.removeEventListener('change', this.handleDecrement);}private render() {this.countSpan.textContent = this.count;}private handleIncrement(e: MouseEvent) {this.count++;}private handleDecrement(e: MouseEvent) {this.count--;}}
typescript// src/components/MyCounter.tsimport {BaseComponent, Component, Prop, Ref} from 'salvo-ts';@Component()export class MyCounter extends BaseComponent {@Prop({default: 0})private _count!: number;get count() {return this._count;}set count(value: number) {this._count = value;// Every time the count changes, render the spanthis.render();}@Ref()private incrementButton!: HTMLElement;@Ref()private decrementButton!: HTMLElement;@Ref()private countSpan!: HTMLElement;ready() {// Register listenersthis.incrementButton.addEventListner('change', this.handleIncrement);this.decrementButton.addEventListner('change', this.handleDecrement);// Render initial count value - note this should _also_ be done in Liquidthis.render();}beforeDestroy() {// Make sure listeners are cleaned up after the component is destroyedthis.incrementButton.removeEventListener('change', this.handleIncrement);this.decrementButton.removeEventListener('change', this.handleDecrement);}private render() {this.countSpan.textContent = this.count;}private handleIncrement(e: MouseEvent) {this.count++;}private handleDecrement(e: MouseEvent) {this.count--;}}
scss// src/components/MyCounter.scss.MyCounter {display: flex;padding: 10px;&__DecrementButton,&__IncrementButton {background: #999;}&__CountSpan {margin: 0 10px;background: #ccc;}}
scss// src/components/MyCounter.scss.MyCounter {display: flex;padding: 10px;&__DecrementButton,&__IncrementButton {background: #999;}&__CountSpan {margin: 0 10px;background: #ccc;}}