|
1 | 1 | import '@abraham/reflection'
|
2 |
| -import { Component, Computed, Hook, Link, Mut, VueComponent } from 'vue3-oop' |
3 |
| -import { createApp, watchSyncEffect } from 'vue' |
4 |
| -import './theme/app.css' |
5 |
| -import { CountService } from './count.service' |
6 |
| -import { SizeService } from './size.service' |
| 2 | +import { Component, Computed, Hook, Mut, VueComponent, VueService } from 'vue3-oop' |
| 3 | +import { createApp } from 'vue' |
7 | 4 |
|
8 |
| -class Child extends VueComponent { |
9 |
| - render() { |
10 |
| - console.log('render') |
11 |
| - return ( |
12 |
| - <div> |
13 |
| - 111 |
14 |
| - <div>{this.context.slots.default?.()}</div> |
15 |
| - </div> |
16 |
| - ) |
| 5 | +const debounce = (delay: number) => { |
| 6 | + let timeout: number | undefined |
| 7 | + return (track: any, trigger: any) => { |
| 8 | + let value: any |
| 9 | + return { |
| 10 | + get() { |
| 11 | + track() |
| 12 | + return value |
| 13 | + }, |
| 14 | + set(newValue: any) { |
| 15 | + clearTimeout(timeout) |
| 16 | + timeout = window.setTimeout(() => { |
| 17 | + value = newValue |
| 18 | + trigger() |
| 19 | + }, delay) |
| 20 | + }, |
| 21 | + } |
17 | 22 | }
|
18 | 23 | }
|
19 | 24 |
|
20 |
| -@Component() |
| 25 | +class CountService extends VueService { |
| 26 | + @Mut() count = 1 |
| 27 | +} |
| 28 | + |
21 | 29 | class Home extends VueComponent {
|
22 |
| - constructor(private countService: CountService, private sizeService: SizeService) { |
23 |
| - super() |
24 |
| - watchSyncEffect(() => this.count > 2) |
25 |
| - } |
| 30 | + @Mut(debounce(1000)) _count = '' |
26 | 31 |
|
27 |
| - @Mut() count = 1 |
| 32 | + @Mut() obj = { name: 123 } |
28 | 33 |
|
29 | 34 | @Computed()
|
30 | 35 | get double() {
|
31 |
| - return this.count * 2 |
| 36 | + return this._count |
| 37 | + } |
| 38 | + set double(val: string) { |
| 39 | + this._count = val.toUpperCase() |
32 | 40 | }
|
33 |
| - list = new Array(10).fill(1) |
34 |
| - |
35 |
| - @Link() aaa: Child[] |
36 | 41 |
|
37 | 42 | @Hook('Mounted')
|
38 | 43 | mounted() {
|
39 |
| - console.log(this.aaa) |
| 44 | + console.log('mounted') |
| 45 | + } |
| 46 | + |
| 47 | + render() { |
| 48 | + console.log(this.obj) |
| 49 | + console.log('render') |
| 50 | + return <input type="text" v-model={this.double} /> |
40 | 51 | }
|
| 52 | +} |
41 | 53 |
|
| 54 | +@Component() |
| 55 | +class Home1 extends Home { |
| 56 | + constructor(private cs: CountService) { |
| 57 | + super() |
| 58 | + console.log(cs) |
| 59 | + } |
| 60 | + @Mut() count1 = 111 |
42 | 61 | render() {
|
43 | 62 | return (
|
44 |
| - <div style={{ textAlign: 'center' }}> |
45 |
| - <Child> |
46 |
| - <div>11111</div> |
47 |
| - </Child> |
48 |
| - <h2>count: {this.countService.count}</h2> |
49 |
| - <button onClick={() => this.countService.add()}>+</button> |
50 |
| - <button onClick={() => this.countService.remove()}>-</button> |
51 |
| - <p> |
52 |
| - 矩形大小: 宽度: {this.sizeService.x} 长度: {this.sizeService.y} |
53 |
| - </p> |
54 |
| - <div |
55 |
| - ref={this.sizeService.target} |
56 |
| - style={{ |
57 |
| - width: '100px', |
58 |
| - height: '100px', |
59 |
| - resize: 'both', |
60 |
| - border: '1px solid #ccc', |
61 |
| - overflow: 'scroll', |
62 |
| - }} |
63 |
| - ></div> |
| 63 | + <div> |
| 64 | + <h5 onClick={() => this.cs.count++}>{this.cs.count}</h5> |
| 65 | + <h2 onClick={() => this.count1++}>1111{this.count1}</h2> |
| 66 | + {super.render()} |
64 | 67 | </div>
|
65 | 68 | )
|
66 | 69 | }
|
67 | 70 | }
|
68 | 71 |
|
69 |
| -const app = createApp(Home) |
| 72 | +const app = createApp(Home1) |
70 | 73 | app.mount('#app')
|
0 commit comments