@@ -40,8 +40,8 @@ npm run dev // 构建 dev 版本
40
40
41
41
``` js
42
42
// component.js
43
- const computedBehavior = require (" miniprogram-computed" ).behavior ;
44
- const behaviorTest = require (" ./behavior-test" ); // 引入自定义 behavior
43
+ const computedBehavior = require (' miniprogram-computed' ).behavior
44
+ const behaviorTest = require (' ./behavior-test' ) // 引入自定义 behavior
45
45
46
46
Component ({
47
47
behaviors: [behaviorTest, computedBehavior],
@@ -53,18 +53,18 @@ Component({
53
53
sum (data ) {
54
54
// 注意: computed 函数中不能访问 this ,只有 data 对象可供访问
55
55
// 这个函数的返回值会被设置到 this.data.sum 字段中
56
- return data .a + data .b + data .c ; // data.c 为自定义 behavior 数据段
56
+ return data .a + data .b + data .c // data.c 为自定义 behavior 数据段
57
57
},
58
58
},
59
59
methods: {
60
60
onTap () {
61
61
this .setData ({
62
62
a: this .data .b ,
63
63
b: this .data .a + this .data .b ,
64
- });
64
+ })
65
65
},
66
66
},
67
- });
67
+ })
68
68
```
69
69
70
70
``` js
@@ -73,7 +73,7 @@ module.exports = Behavior({
73
73
data: {
74
74
c: 2 ,
75
75
},
76
- });
76
+ })
77
77
```
78
78
79
79
``` xml
@@ -86,7 +86,7 @@ module.exports = Behavior({
86
86
### watch 基本用法
87
87
88
88
``` js
89
- const computedBehavior = require (" miniprogram-computed" ).behavior ;
89
+ const computedBehavior = require (' miniprogram-computed' ).behavior
90
90
91
91
Component ({
92
92
behaviors: [computedBehavior],
@@ -96,21 +96,21 @@ Component({
96
96
sum: 2 ,
97
97
},
98
98
watch: {
99
- " a, b " : function (a , b ) {
99
+ ' a, b ' : function (a , b ) {
100
100
this .setData ({
101
101
sum: a + b,
102
- });
102
+ })
103
103
},
104
104
},
105
105
methods: {
106
106
onTap () {
107
107
this .setData ({
108
108
a: this .data .b ,
109
109
b: this .data .a + this .data .b ,
110
- });
110
+ })
111
111
},
112
112
},
113
- });
113
+ })
114
114
```
115
115
116
116
``` xml
@@ -125,7 +125,7 @@ Component({
125
125
由于通过 behavior 的方式引入不能获得类型支持, 因此为了获得类型的支持, 可以使用一个辅助组件构造器:
126
126
127
127
``` ts
128
- import { ComponentWithComputed } from " miniprogram-computed" ;
128
+ import { ComponentWithComputed } from ' miniprogram-computed'
129
129
130
130
ComponentWithComputed ({
131
131
data: {
@@ -134,20 +134,20 @@ ComponentWithComputed({
134
134
sum: 2 ,
135
135
},
136
136
watch: {
137
- " a, b" : function (a , b ) {
137
+ ' a, b' : function (a , b ) {
138
138
this .setData ({
139
139
sum: a + b ,
140
- });
140
+ })
141
141
},
142
142
},
143
143
computed: {
144
144
sum(data ) {
145
145
// 注意: computed 函数中不能访问 this ,只有 data 对象可供访问
146
146
// 这个函数的返回值会被设置到 this.data.sum 字段中
147
- return data .a + data .b + data .sum ; // data.c 为自定义 behavior 数据段
147
+ return data .a + data .b + data .sum // data.c 为自定义 behavior 数据段
148
148
},
149
149
},
150
- });
150
+ })
151
151
```
152
152
153
153
当使用该构造器的时候, 编译器可以给 ` computed ` 和 ` watch ` 提供自动提示和类型支持。
@@ -158,7 +158,7 @@ ComponentWithComputed({
158
158
159
159
** 关于 TS 兼容问题**
160
160
161
- 若在小程序中用 ` TypeScript ` 进行开发并使用到了 ` Component ` 构造器。这时定义 ` computed ` 或 ` watch ` 字段会出现类型报错。
161
+ 若在小程序中用 ` TypeScript ` 进行开发并使用到了 ` Component ` 构造器。这时定义 ` computed ` 或 ` watch ` 字段会出现类型报错。
162
162
163
163
针对此问题,推荐使用 ` ComponentWithComputed ` 构造器代替 ` Component ` 构造器。
164
164
@@ -215,7 +215,7 @@ ComponentWithComputed({
215
215
在 ` watch ` 字段上可以使用 ` ** ` 通配符,是它能够监听这个字段下的子字段的变化(类似于小程序基础库本身的 observers )。
216
216
217
217
``` js
218
- const computedBehavior = require (" miniprogram-computed" ).behavior ;
218
+ const computedBehavior = require (' miniprogram-computed' ).behavior
219
219
220
220
Component ({
221
221
behaviors: [computedBehavior],
@@ -226,20 +226,20 @@ Component({
226
226
},
227
227
},
228
228
watch: {
229
- " obj.** " : function (obj ) {
229
+ ' obj.** ' : function (obj ) {
230
230
this .setData ({
231
231
sum: obj .a + obj .b ,
232
- });
232
+ })
233
233
},
234
234
},
235
235
methods: {
236
236
onTap () {
237
237
this .setData ({
238
- " obj.a" : 10 ,
239
- });
238
+ ' obj.a' : 10 ,
239
+ })
240
240
},
241
241
},
242
- });
242
+ })
243
243
```
244
244
245
245
除此以外:
@@ -248,4 +248,5 @@ Component({
248
248
- 对于使用了 ` ** ` 通配符的字段,则会进行深比较,来尝试精确检测对象是否真的发生了变化,这要求对象字段不能包含循环(类似于 ` JSON.stringify ` )。
249
249
250
250
### 关于低版本兼容
251
- 对于 IOS ` 9.3 ` 以下的版本,由于无法原生支持 ` Proxy ` ,这里会使用 ` proxy-polyfill ` 去代替。
251
+
252
+ 对于 IOS ` 9.3 ` 以下的版本,由于无法原生支持 ` Proxy ` ,这里会使用 ` proxy-polyfill ` 去代替。
0 commit comments