-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini-program-2.html
610 lines (580 loc) · 67.4 KB
/
mini-program-2.html
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>微信小程序知识点(二) | 小李の博客</title><meta name="keywords" content="小程序,前端"><meta name="author" content="Pride_Lee"><meta name="copyright" content="Pride_Lee"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="微信小程序知识点二">
<meta property="og:type" content="article">
<meta property="og:title" content="微信小程序知识点(二)">
<meta property="og:url" content="https://lhx.zzux.com/mini-program-2.html">
<meta property="og:site_name" content="小李の博客">
<meta property="og:description" content="微信小程序知识点二">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s1.ax1x.com/2022/07/05/jNjg4s.png">
<meta property="article:published_time" content="2022-11-28T12:13:10.000Z">
<meta property="article:modified_time" content="2022-11-28T16:55:16.440Z">
<meta property="article:author" content="Pride_Lee">
<meta property="article:tag" content="前端">
<meta property="article:tag" content="小程序">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s1.ax1x.com/2022/07/05/jNjg4s.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://lhx.zzux.com/mini-program-2"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google-site-verification" content="gHN_afDedvo00U6YhKTblmdcWb6sw0-3uRkGD1xwr28"/><meta name="baidu-site-verification" content="code-kFbZe5nv4p"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
root: '/',
algolia: undefined,
localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容:${query}"}},
translate: undefined,
noticeOutdate: undefined,
highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
copy: {
success: '复制成功',
error: '复制错误',
noSupport: '浏览器不支持'
},
relativeDate: {
homepage: false,
post: false
},
runtime: '',
date_suffix: {
just: '刚刚',
min: '分钟前',
hour: '小时前',
day: '天前',
month: '个月前'
},
copyright: {"limitCount":50,"languages":{"author":"作者: Pride_Lee","link":"链接: ","source":"来源: 小李の博客","info":"著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。"}},
lightbox: 'fancybox',
Snackbar: undefined,
source: {
justifiedGallery: {
js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
}
},
isPhotoFigcaption: false,
islazyload: false,
isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
title: '微信小程序知识点(二)',
isPost: true,
isHome: false,
isHighlightShrink: false,
isToc: true,
postUpdate: '2022-11-29 00:55:16'
}</script><noscript><style type="text/css">
#nav {
opacity: 1
}
.justified-gallery img {
opacity: 1
}
#recent-posts time,
#post-meta time {
display: inline !important
}
</style></noscript><script>(win=>{
win.saveToLocal = {
set: function setWithExpiry(key, value, ttl) {
if (ttl === 0) return
const now = new Date()
const expiryDay = ttl * 86400000
const item = {
value: value,
expiry: now.getTime() + expiryDay,
}
localStorage.setItem(key, JSON.stringify(item))
},
get: function getWithExpiry(key) {
const itemStr = localStorage.getItem(key)
if (!itemStr) {
return undefined
}
const item = JSON.parse(itemStr)
const now = new Date()
if (now.getTime() > item.expiry) {
localStorage.removeItem(key)
return undefined
}
return item.value
}
}
win.getScript = url => new Promise((resolve, reject) => {
const script = document.createElement('script')
script.src = url
script.async = true
script.onerror = reject
script.onload = script.onreadystatechange = function() {
const loadState = this.readyState
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
script.onload = script.onreadystatechange = null
resolve()
}
document.head.appendChild(script)
})
win.activateDarkMode = function () {
document.documentElement.setAttribute('data-theme', 'dark')
if (document.querySelector('meta[name="theme-color"]') !== null) {
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
}
}
win.activateLightMode = function () {
document.documentElement.setAttribute('data-theme', 'light')
if (document.querySelector('meta[name="theme-color"]') !== null) {
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
}
}
const t = saveToLocal.get('theme')
if (t === 'dark') activateDarkMode()
else if (t === 'light') activateLightMode()
const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
if (asideStatus === 'hide') {
document.documentElement.classList.add('hide-aside')
} else {
document.documentElement.classList.remove('hide-aside')
}
}
const detectApple = () => {
if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
document.documentElement.classList.add('apple')
}
}
detectApple()
})(window)</script><meta name="generator" content="Hexo 6.2.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s1.ax1x.com/2022/07/05/jNgtYj.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">19</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">13</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">14</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间线</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s1.ax1x.com/2022/07/05/jNjg4s.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小李の博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间线</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">微信小程序知识点(二)</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-11-28T12:13:10.000Z" title="发表于 2022-11-28 20:13:10">2022-11-28</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-11-28T16:55:16.440Z" title="更新于 2022-11-29 00:55:16">2022-11-29</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%89%8D%E7%AB%AF/">前端</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%89%8D%E7%AB%AF/%E9%9D%A2%E8%AF%95%E9%A2%98/">面试题</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>12分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="微信小程序知识点(二)"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="小程序数据绑定"><a href="#小程序数据绑定" class="headerlink" title="小程序数据绑定"></a>小程序数据绑定</h2><h3 id="定义数据"><a href="#定义数据" class="headerlink" title="定义数据"></a>定义数据</h3><ul>
<li><p>在 data 中定义数据</p>
<blockquote>
<p>在页面对应的 .js 文件中,把数据定义到data对象中即可</p>
</blockquote>
</li>
<li><p>在 WXML 中使用数据</p>
<blockquote>
<p>把data中的数据绑定到页面中渲染,使用 Mustache 语法(双大括号)将变量包起来即可。</p>
<p><view> </view></p>
<p>图片的src属性也可以使用 Mustache 语法</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Page</span>({</span><br><span class="line"> <span class="attr">data</span>:{</span><br><span class="line"> <span class="attr">imgSrc</span>:<span class="string">'http://xxxxxxx.png'</span></span><br><span class="line"> }</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="comment">//页面结构如下</span></span><br><span class="line"><image src=<span class="string">"{{imgSrc}}"</span>></image></span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="事件绑定"><a href="#事件绑定" class="headerlink" title="事件绑定"></a>事件绑定</h2><table>
<thead>
<tr>
<th align="center">类型</th>
<th align="center">绑定方式</th>
<th align="center">事件描述</th>
</tr>
</thead>
<tbody><tr>
<td align="center">tap</td>
<td align="center">bindtap 或 bind:tap</td>
<td align="center">手指触摸后马上离开,类似HTML中 click 点击事件</td>
</tr>
<tr>
<td align="center">input</td>
<td align="center">bindinput 或 bind:input</td>
<td align="center">文本框的输入事件</td>
</tr>
<tr>
<td align="center">change</td>
<td align="center">bindchange 或 bind:change</td>
<td align="center">状态改变时触发</td>
</tr>
</tbody></table>
<h3 id="事件对象的属性列表"><a href="#事件对象的属性列表" class="headerlink" title="事件对象的属性列表"></a>事件对象的属性列表</h3><blockquote>
<p>当事件回调触发的时候,会收到一个事件对象 event ,它的详细属性如下表所示:</p>
</blockquote>
<table>
<thead>
<tr>
<th align="center">属性</th>
<th align="center">类型</th>
<th align="center">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">type</td>
<td align="center">string</td>
<td align="center">事件类型</td>
</tr>
<tr>
<td align="center">timeStamp</td>
<td align="center">integer</td>
<td align="center">页面打开到触发事件所经过的毫秒数</td>
</tr>
<tr>
<td align="center"><em><strong>target</strong></em></td>
<td align="center">Object</td>
<td align="center"><em><strong>触发事件的组件的一些属性值集合</strong></em></td>
</tr>
<tr>
<td align="center">currentTarget</td>
<td align="center">Object</td>
<td align="center">当前组件的一些属性值集合</td>
</tr>
<tr>
<td align="center"><strong>detail</strong></td>
<td align="center">Object</td>
<td align="center"><strong>额外的信息</strong></td>
</tr>
<tr>
<td align="center">touches</td>
<td align="center">Array</td>
<td align="center">触摸事件,当前停留在屏幕中的触摸点信息的数组</td>
</tr>
<tr>
<td align="center">changedTouches</td>
<td align="center">Array</td>
<td align="center">触摸事件,当前变化的触摸点信息的数组</td>
</tr>
</tbody></table>
<h3 id="target-和-currentTarget的区别"><a href="#target-和-currentTarget的区别" class="headerlink" title="target 和 currentTarget的区别"></a>target 和 currentTarget的区别</h3><blockquote>
<p>target是触发该事件的源头组件,而currentTarget 则是当前事件所绑定的组件。</p>
<p><img src="https://s1.ax1x.com/2022/11/28/zagFyt.png" alt="事件"></p>
<p>点击内部的按钮时,点击事件以冒泡的方式向外扩散,也会触发外层view的tap事件处理函数。</p>
<p>此时对于外层的view来说:</p>
<p>e.target指向的是触发事件的源头组件,因此,e.target是内部的按钮组件<br>e.currentTarget指向的是当前正在触发事件的那个组件,因此,e.currentTarget是当前的view组件</p>
<p>大白话来说:因为点了button按钮,然后向上冒泡才执行了bindtap,源头是button。</p>
</blockquote>
<h3 id="bindtap的语法格式"><a href="#bindtap的语法格式" class="headerlink" title="bindtap的语法格式"></a>bindtap的语法格式</h3><p>在小程序中,不存在HTML中的onclick鼠标点击事件,而是通过tap事件来响应用户的触摸行为。</p>
<ol>
<li><p>通过bindtap,可以为组件绑定tap触摸事件,语法如下:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">button</span> <span class="attr">type</span>=<span class="string">"primary"</span> <span class="attr">bindtap</span>=<span class="string">"btnTapHandler"</span>></span> 按钮 <span class="tag"></<span class="name">button</span>></span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在页面的js文件中定义对应的事件处理函数,事件参数通过形参event(一般简写成e)来接收:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Page</span>({</span><br><span class="line"> <span class="title function_">btnTapHandler</span>(<span class="params">e</span>){ <span class="comment">//按钮的tap事件处理函数</span></span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(e) <span class="comment">//事件参数对象 e</span></span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="在事件处理函数中为-data-中的数据赋值"><a href="#在事件处理函数中为-data-中的数据赋值" class="headerlink" title="在事件处理函数中为 data 中的数据赋值"></a>在事件处理函数中为 data 中的数据赋值</h3><p>通过调用this.setData(dataObject)方法,可以给页面data中的数据重新赋值,示例如下:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Page({</span><br><span class="line"> data:{</span><br><span class="line"> count:0</span><br><span class="line">},</span><br><span class="line">//修改count的值</span><br><span class="line">changeCount(){</span><br><span class="line"> this.setData({</span><br><span class="line"> count:this.data.count 1</span><br><span class="line">)</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h3 id="事件传参"><a href="#事件传参" class="headerlink" title="事件传参"></a>事件传参</h3><p>小程序中的事件传参比较特殊,不能在绑定事件的同时为事件处理函数传递参数。例如,下面的代码将不能正常工作:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">button</span> <span class="attr">type</span>=<span class="string">"primary"</span> <span class="attr">bindtap</span>=<span class="string">'btnHandler(123)'</span>></span>事件传参<span class="tag"></<span class="name">button</span>></span></span><br></pre></td></tr></table></figure>
<p>因为小程序会把bindtap的属性值,统一当作事件名称来处理,相当于要调用一个名称为btnHandler(123)的事件处理函数。</p>
<blockquote>
<p>可以为组件提供data-* 自定义属性传参,其中*代表的是参数的名字,示例代码如下:</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">button</span> <span class="attr">bindtap</span>=<span class="string">"btnHandler"</span><span class="attr">data-info</span>=<span class="string">"{{2}}"</span>></span>事件传参<span class="tag"></<span class="name">button</span>></span></span><br></pre></td></tr></table></figure>
<p>最终:</p>
<ul>
<li>info 会被解析为 参数的名字</li>
<li>数值 2 会被解析为参数的值</li>
</ul>
<p>在事件处理函数中,通过event.target.dataset..参数名即可获取到具体参数的值,示例代码如下:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">btnHandler</span>(<span class="params">event</span>){</span><br><span class="line"><span class="comment">//dataset是一个对象,包含了所有通过data-*传递过来的参数项</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(event.<span class="property">target</span>.<span class="property">dataset</span>)</span><br><span class="line"><span class="comment">//通过dataset可以访问到具体参数的值</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(event.<span class="property">target</span>.<span class="property">dataset</span>.<span class="property">info</span>)</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="bindinput语法"><a href="#bindinput语法" class="headerlink" title="bindinput语法"></a>bindinput语法</h3><p>在小程序中,通过input事件来响应文本框的输入事件,语法格式如下:</p>
<ol>
<li><p>通过bindinput,可以为文本框绑定输入事件:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">input</span> <span class="attr">bindinput</span>=<span class="string">"inputHandler"</span>></span><span class="tag"><<span class="name">input</span>></span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在页面的jS文件中定义事件处理函数:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">inputHandler</span>(<span class="params">e</span>){</span><br><span class="line"><span class="comment">//e.detail.value是变化过后,文本框最新的值</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(e.<span class="property">detail</span>.<span class="property">value</span>)</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="文本框和-data-数据同步"><a href="#文本框和-data-数据同步" class="headerlink" title="文本框和 data 数据同步"></a>文本框和 data 数据同步</h3><ol>
<li><p>定义数据</p>
</li>
<li><p>渲染结构</p>
</li>
<li><p>绑定input事件处理函数</p>
<p>代码如下:</p>
</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><input value="{{msg}}"bindinput="inputHandler"></input></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//文本框内容改变的事件</span></span><br><span class="line"><span class="title function_">iptHandler</span>(<span class="params">e</span>){</span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">setData</span>({</span><br><span class="line"> <span class="comment">//通过e.detail.value获取到文本框最新的值</span></span><br><span class="line"> <span class="attr">msg</span>:e.<span class="property">detail</span>.<span class="property">value</span></span><br><span class="line"> })</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="条件渲染"><a href="#条件渲染" class="headerlink" title="条件渲染"></a>条件渲染</h2><p>在小程序中,使用wx:if=”{condition}”来判断是否需要渲染该代码块:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">view</span> <span class="attr">wx:if</span>=<span class="string">"{{condition}}"</span>></span>True <span class="tag"></<span class="name">view</span>></span></span><br></pre></td></tr></table></figure>
<p>也可以用 wx:elif 和 wx:else 来添加else判断</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">view</span> <span class="attr">wx:if</span>=<span class="string">"{{type ==1}}"</span>></span> 男 <span class="tag"></<span class="name">view</span>></span></span><br><span class="line"><span class="tag"><<span class="name">view</span> <span class="attr">wx:elif</span>=<span class="string">"{{type ==2}}"</span>></span> 女 <span class="tag"></<span class="name">view</span>></span></span><br><span class="line"><span class="tag"><<span class="name">view</span> <span class="attr">wx:else</span>></span> 保密 <view</span><br></pre></td></tr></table></figure>
<h3 id="结合-lt-block-gt-使用-wx-if"><a href="#结合-lt-block-gt-使用-wx-if" class="headerlink" title="结合 < block > 使用 wx:if"></a>结合 < block > 使用 wx:if</h3><p>如果要一次性控制多个组件的展示与隐藏,可以使用一个<block></block>标签将多个组件包装起来,并在< block >标签上使用 wx:if 控制属性,示例如下:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">block</span> <span class="attr">wx:if</span>=<span class="string">"{{true}}"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">view</span>></span>view1 <span class="tag"></<span class="name">view</span>></span></span><br><span class="line"><span class="tag"><<span class="name">view</span>></span>view2 <span class="tag"></<span class="name">view</span>></span></span><br><span class="line"><span class="tag"></<span class="name">block</span>></span></span><br></pre></td></tr></table></figure>
<p>注意:< block >并不是一个组件,它只是一个包裹性质的容器,不会在页面中做任何渲染,我们只会看到view。</p>
<h3 id="hidden"><a href="#hidden" class="headerlink" title="hidden"></a>hidden</h3><p>在小程序中,直接使用hidden=”“也能控制元素的显示与隐藏:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">view</span> <span class="attr">hidden</span>=<span class="string">"{{condition}}"</span>></span>条件为true陷藏,条件为false显示<span class="tag"></<span class="name">view</span>></span></span><br></pre></td></tr></table></figure>
<h3 id="wx-if-与-hidden-的对比"><a href="#wx-if-与-hidden-的对比" class="headerlink" title="wx:if 与 hidden 的对比"></a>wx:if 与 hidden 的对比</h3><ol>
<li>运行方式不同<ul>
<li>wx:if 以 <strong>动态创建和移除元素</strong> 的方式,控制元素的展示与隐藏</li>
<li>hidden <strong>以切换样式</strong>的方式(display: none/block;)控制元素的显示与隐藏</li>
</ul>
</li>
<li>使用建议<ul>
<li>频繁切换时,建议使用hidden</li>
<li>控制条件复杂时,建议使用wx:if搭配wx:elif、wx:else进行展示与隐藏的切换</li>
</ul>
</li>
</ol>
<h2 id="列表渲染"><a href="#列表渲染" class="headerlink" title="列表渲染"></a>列表渲染</h2><h3 id="wx-for"><a href="#wx-for" class="headerlink" title="wx:for"></a>wx:for</h3><p>通过wx:for可以根据指定的数组,循环渲染重复的组件结构,语法示例如下:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">view</span> <span class="attr">wx:for</span>=<span class="string">"{{array}}"</span>></span></span><br><span class="line">索引是:{{index}} 当前项是:{{item}}</span><br><span class="line"><span class="tag"></<span class="name">view</span>></span></span><br></pre></td></tr></table></figure>
<h3 id="手动指定索引和当前项的变量名"><a href="#手动指定索引和当前项的变量名" class="headerlink" title="手动指定索引和当前项的变量名"></a>手动指定索引和当前项的变量名</h3><ul>
<li>使用 wx:for-index 可以指定<strong>当前循环项的索引</strong>的变量名</li>
<li>使用 wx:for-item 可以指定当前项的变量名</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">view</span> <span class="attr">wx:for</span>=<span class="string">"{{array}}"</span> <span class="attr">wx:for-index</span>=<span class="string">"idx” wx:for-item="</span><span class="attr">itemName</span>"></span></span><br><span class="line"> 索引是:{{idx}} 当前项是:{{itemName}}</span><br><span class="line"> <span class="tag"></<span class="name">view</span>></span></span><br></pre></td></tr></table></figure>
<h3 id="wx-key的使用"><a href="#wx-key的使用" class="headerlink" title="wx:key的使用"></a>wx:key的使用</h3><p>类似于Vue 列表渲染中的 :key,小程序在实现列表渲染时,也建议为渲染出来的列表项指定唯一的key值,<br>从而提高渲染的效率,示例代码如下:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// data 数据</span></span><br><span class="line"> <span class="attr">data</span>:{</span><br><span class="line"> <span class="attr">userList</span>: [</span><br><span class="line"> {<span class="attr">id</span>:<span class="number">1</span>, <span class="attr">name</span>:<span class="string">'小红’},</span></span><br><span class="line"><span class="string"> {id:2, name:'</span>小黄’},</span><br><span class="line"> {<span class="attr">id</span>:<span class="number">3</span>,<span class="attr">name</span>: <span class="string">'小白’}</span></span><br><span class="line"><span class="string"> ]</span></span><br><span class="line"><span class="string">}</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> // wxml 结构</span></span><br><span class="line"><span class="string"> <view wx:for="{{userList}}" wx:key="id">{{item.name}}</view></span></span><br></pre></td></tr></table></figure>
<h2 id="WXSS"><a href="#WXSS" class="headerlink" title="WXSS"></a>WXSS</h2><h3 id="WXSS-和-CSS的关系"><a href="#WXSS-和-CSS的关系" class="headerlink" title="WXSS 和 CSS的关系"></a>WXSS 和 CSS的关系</h3><p>WXSS具有CSS大部分特性,同时,WXSS还对CSS进行了扩充以及修改,以适应微信小程序的开发。</p>
<p>与CSS相比,WXSS 扩展的特性有:</p>
<ul>
<li><p>rpx尺寸单位</p>
<blockquote>
<p>rpx(responsive pixel)是微信小程序独有的,用来解决屏适配的尺寸单位。</p>
<p>原理:把所有设备的屏幕在宽度上等分为750份,小程序在不同设备上运行的时候,会自动把rpx的样式单位换算成对应的像素单位来渲染。</p>
</blockquote>
</li>
<li><p>@import样式导入</p>
<blockquote>
<p>@import 后跟需要导入的外联样式表的相对路径,用;表示语句结束。示例如下:</p>
</blockquote>
</li>
</ul>
<p> </p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**比如有一个common.wxss**/</span></span><br><span class="line">-------</span><br><span class="line"><span class="comment">/**在app.wxss中**/</span></span><br><span class="line"><span class="keyword">@import</span> ”common.wxss“;</span><br><span class="line"><span class="comment">/**注意要用分号结尾**/</span></span><br><span class="line"> <span class="selector-class">.middle-p</span>{</span><br><span class="line"> <span class="attribute">padding</span>:<span class="number">15px</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="全局样式、局部样式"><a href="#全局样式、局部样式" class="headerlink" title="全局样式、局部样式"></a>全局样式、局部样式</h3><ol>
<li><p>全局样式</p>
<blockquote>
<p>定义在 app.wxss 中的样式为全局样式,作用于每一个页面。</p>
</blockquote>
</li>
<li><blockquote>
<p>在页面的.Wxss文件中定义的样式为局部样式,只作用于当前页面。</p>
<p>① 当局部样式和全局样式冲突时,根据就近原则,局部样式会覆盖全局样式<br>② 当局部样式的权重大于或等于全局样式的权重时,才会覆盖全局的样式</p>
</blockquote>
</li>
</ol>
<h2 id="全局配置"><a href="#全局配置" class="headerlink" title="全局配置"></a>全局配置</h2><h3 id="全局配置文件"><a href="#全局配置文件" class="headerlink" title="全局配置文件"></a>全局配置文件</h3><p>小程序根目录下的 app.json 文件是小程序的全局配置文件。常用的配置项如下:</p>
<ul>
<li><p>pages</p>
<p>记录当前小程序所有页面的存放路径</p>
</li>
<li><p>window</p>
<p>全局设置小程序窗口的外观</p>
</li>
<li><p>tabBar</p>
<p>设置小程序底部的 tabBar 效果</p>
</li>
<li><p>style</p>
<p>是否启用新版的组件样式</p>
</li>
</ul>
<h3 id="window节点常用配置"><a href="#window节点常用配置" class="headerlink" title="window节点常用配置"></a>window节点常用配置</h3><table>
<thead>
<tr>
<th>属性名</th>
<th>类型</th>
<th>默认值</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>navigationBarTitleText</td>
<td>String</td>
<td>字符串</td>
<td>导航栏标题文字内容</td>
</tr>
<tr>
<td>navigationBarBackgroundColor</td>
<td>HexColor</td>
<td>#000000</td>
<td>导航栏背景颜色,如#000000</td>
</tr>
<tr>
<td>navigationBarTextStyle</td>
<td>String</td>
<td>white</td>
<td>导航栏标题颜色,仅支持 black/ white</td>
</tr>
<tr>
<td>backgroundColor</td>
<td>HexColor</td>
<td>#ffffff</td>
<td>窗口的背景色</td>
</tr>
<tr>
<td>backgroundTextStyle</td>
<td>String</td>
<td>dark</td>
<td>下拉loading 的样式,仅支持 dark / light</td>
</tr>
<tr>
<td>enablePullDownRefresh</td>
<td>Boolean</td>
<td>false</td>
<td>是否全局开启下拉刷新</td>
</tr>
<tr>
<td>onReachBottomDistance</td>
<td>Number</td>
<td>50</td>
<td>页面上拉触底事件触发时距页面底部距离,单位为px</td>
</tr>
</tbody></table>
<h3 id="模拟器的问题"><a href="#模拟器的问题" class="headerlink" title="模拟器的问题"></a>模拟器的问题</h3><p>模拟器会有bug,并不能百分百还原手机上的效果,以真机为准。</p>
<h3 id="上拉触底的距离"><a href="#上拉触底的距离" class="headerlink" title="上拉触底的距离"></a>上拉触底的距离</h3><blockquote>
<p>概念:上拉触底是移动端的专有名词,通过手指在屏幕上的上拉滑动操作,从而加载更多数据的行为。</p>
</blockquote>
<p>设置步骤:app.json -> window -> 为 onReachBottomDistance设置新的数值</p>
<p>注意:默认距离为50px,如果没有特殊需求,建议使用默认值即可。</p>
<h3 id="tabBar"><a href="#tabBar" class="headerlink" title="tabBar"></a>tabBar</h3><p>tabBar是移动端应用常见的页面效果,用于实现多页面的快速切换。小程序中通常将其分为:</p>
<ul>
<li><p>底部 tabBar</p>
</li>
<li><p>顶部 tabBar</p>
</li>
</ul>
<p>tabBar中只能配置最少2个、最多5个tab页签。当渲染顶部 tabBar 时,不显示icon,只显示文本</p>
<h3 id="tabBar的6个组成部分"><a href="#tabBar的6个组成部分" class="headerlink" title="tabBar的6个组成部分"></a>tabBar的6个组成部分</h3><ul>
<li>backgroundColor:tabBar 的背景色</li>
<li>selectedlconPath:选中时的图片路径</li>
<li>borderStyle:tabBar上边框的颜色</li>
<li>iconPath:未选中时的图片路径</li>
<li>selectedColor:tab上的文字选中时的颜色</li>
<li>color tab上文字的默认(未选中)颜色</li>
</ul>
<h3 id="tabBar配置"><a href="#tabBar配置" class="headerlink" title="tabBar配置"></a>tabBar配置</h3><p>在app.json中添加 “tabBar”:{}, 配置对象,配置项如下:</p>
<table>
<thead>
<tr>
<th>属性</th>
<th>类型</th>
<th>必填</th>
<th>默认值</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>position</td>
<td>String</td>
<td>否</td>
<td>bottom</td>
<td>tabBar 的位置,仅支持 bottom/top</td>
</tr>
<tr>
<td>borderStyle</td>
<td>String</td>
<td>否</td>
<td>black</td>
<td>tabBar上边框的颜色,仅支持 black/white</td>
</tr>
<tr>
<td>color</td>
<td>HexColor</td>
<td>否</td>
<td></td>
<td>tab上文字的默认(未选中)颜色</td>
</tr>
<tr>
<td>selectedColor</td>
<td>HexColor</td>
<td>否</td>
<td></td>
<td>tab上的文字选中时的颜色</td>
</tr>
<tr>
<td>backgroundColor</td>
<td>HexColor</td>
<td>否</td>
<td></td>
<td>tabBar 的背景色</td>
</tr>
<tr>
<td>list</td>
<td>Array</td>
<td>是</td>
<td></td>
<td>最少2个、最多5个tab</td>
</tr>
</tbody></table>
<p>每个tab项的配置选项</p>
<table>
<thead>
<tr>
<th>属性</th>
<th>类型</th>
<th>必填</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>pagePath</td>
<td>String</td>
<td>是</td>
<td>页面路径,页面必须在 pages 中预先定义</td>
</tr>
<tr>
<td>text</td>
<td>String</td>
<td>是</td>
<td>tab上显示的文字</td>
</tr>
<tr>
<td>iconPath</td>
<td>String</td>
<td>否</td>
<td>未选中时的图标路径;当postion 为top 时,不显示icon</td>
</tr>
<tr>
<td>selectedlconPath</td>
<td>String</td>
<td>否</td>
<td>选中时的图标路径;当postion为top时,不显示 icon</td>
</tr>
</tbody></table>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"tabBar"</span>:{</span><br><span class="line"> <span class="string">"list"</span>:[</span><br><span class="line"> {</span><br><span class="line"> <span class="string">"pagePath"</span>: <span class="string">"pages/index/index"</span>,</span><br><span class="line"> <span class="string">"text"</span>: <span class="string">"index"</span></span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> <span class="string">"pagePath"</span>: <span class="string">"pages/test/test"</span>,</span><br><span class="line"> <span class="string">"text"</span>: <span class="string">"test"</span></span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line">},</span><br></pre></td></tr></table></figure>
<h2 id="数据请求"><a href="#数据请求" class="headerlink" title="数据请求"></a>数据请求</h2><h3 id="小程序中的限制"><a href="#小程序中的限制" class="headerlink" title="小程序中的限制"></a>小程序中的限制</h3><p>出于安全性方面的考虑,小程序官方对数据接口的请求做出了如下两个限制:</p>
<p>①只能请求HTTPS类型的接口</p>
<blockquote>
<p>在开发过程中,在详情—>本地设置中可以勾选 不校验合法域名….即可解决此问题</p>
</blockquote>
<p>② 必须将接口的域名添加到信任列表中</p>
<p>需求描述:假设在自己的微信小程序中,希望请求 <a href="https://lhx.zzux.com/%E5%9F%9F%E5%90%8D%E4%B8%8B%E7%9A%84%E6%8E%A5%E5%8F%A3">https://lhx.zzux.com/域名下的接口</a><br>配置步骤:登录微信小程序管理后台-→开发→开发设置-→服务器域名->修改request合法域名</p>
<p>注意事项:</p>
<p>① 域名只支持https 协议<br>② 域名不能使用IP地址或localhost<br>③ 域名必须经过 ICP 备案<br>④ 服务器域名一个月内最多可申请 5次修改</p>
<h3 id="发get-x2F-post请求"><a href="#发get-x2F-post请求" class="headerlink" title="发get/post请求"></a>发get/post请求</h3><p>调用微信小程序提供的 wx.request() 方法,可以发起 GET 请求,示例如下:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">wx.<span class="title function_">request</span>({</span><br><span class="line"> <span class="attr">url</span>: <span class="string">'https://lhx.zzux.com/api/get'</span>,<span class="comment">// 请求的接口地址,必须基于 https 协议</span></span><br><span class="line"> <span class="attr">method</span>: <span class="string">'GET'</span>,</span><br><span class="line"> <span class="comment">//发post请求同理,只要把method改为post即可</span></span><br><span class="line"> <span class="attr">data</span>:{ <span class="comment">// 发送到服务器的数据</span></span><br><span class="line"> name:<span class="string">'zs'</span>,</span><br><span class="line"> <span class="attr">age</span>:<span class="number">22</span></span><br><span class="line"> },</span><br><span class="line"> <span class="attr">success</span>:<span class="function">(<span class="params">res</span>)=></span>{ <span class="comment">//请求成功之后的回调函数</span></span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h3 id="页面加载时发请求"><a href="#页面加载时发请求" class="headerlink" title="页面加载时发请求"></a>页面加载时发请求</h3><p>在很多情况下,我们需要在页面刚加载的时候,自动请求一些初始化的数据。此时需要在页面的 onLoad事件中调用获取数据的函数,示例代码如下:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">onLoad</span>: <span class="keyword">function</span>(<span class="params">options</span>){</span><br><span class="line"> <span class="comment">//要加this</span></span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">getSwiperList</span>()</span><br><span class="line"> <span class="variable language_">this</span>.<span class="title function_">getGridList</span>()</span><br><span class="line">},</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://lhx.zzux.com">Pride_Lee</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://lhx.zzux.com/mini-program-2.html">https://lhx.zzux.com/mini-program-2.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://lhx.zzux.com" target="_blank">小李の博客</a>!</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%89%8D%E7%AB%AF/">前端</a><a class="post-meta__tags" href="/tags/%E5%B0%8F%E7%A8%8B%E5%BA%8F/">小程序</a></div><div class="post_share"><div class="social-share" data-image="https://s1.ax1x.com/2022/07/05/jNjg4s.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wechat.jpg" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/img/alipay.jpg" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/jd-yangmao.html"><img class="prev-cover" src="https://s1.ax1x.com/2022/07/05/jNqUyD.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">全自动薅JD羊毛</div></div></a></div><div class="next-post pull-right"><a href="/mini-program.html"><img class="next-cover" src="https://s1.ax1x.com/2022/07/05/jNqUyD.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">微信小程序知识点(一)</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/mini-program.html" title="微信小程序知识点(一)"><img class="cover" src="https://s1.ax1x.com/2022/07/05/jNqUyD.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-27</div><div class="title">微信小程序知识点(一)</div></div></a></div><div><a href="/fe-css.html" title="前端面试题--css篇"><img class="cover" src="https://s1.ax1x.com/2022/07/22/jLbI8P.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-07-21</div><div class="title">前端面试题--css篇</div></div></a></div><div><a href="/fe-mianshi.html" title="前端综合面试题"><img class="cover" src="https://s1.ax1x.com/2022/07/05/jNjg4s.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-25</div><div class="title">前端综合面试题</div></div></a></div><div><a href="/fe-bfc.html" title="什么是BFC?它的作用是什么以及怎么创建BFC区域"><img class="cover" src="https://s1.ax1x.com/2022/07/22/jOrC59.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-08-22</div><div class="title">什么是BFC?它的作用是什么以及怎么创建BFC区域</div></div></a></div><div><a href="/FE-html.html" title="前端面试题--html篇"><img class="cover" src="https://s1.ax1x.com/2022/07/21/jqyBjK.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-07-20</div><div class="title">前端面试题--html篇</div></div></a></div><div><a href="/data-type.html" title="js数据类型和数据类型检测"><img class="cover" src="https://s1.ax1x.com/2022/07/05/jNqUyD.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-07-26</div><div class="title">js数据类型和数据类型检测</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div><div id="comment-switch"><span class="first-comment">Valine</span><span class="switch-btn"></span><span class="second-comment">Livere</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div><div><div id="lv-container" data-id="city" data-uid="MTAyMC81Njc0OS8zMzIxMw=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://s1.ax1x.com/2022/07/05/jNgtYj.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Pride_Lee</div><div class="author-info__description">99年的Fe(前端)小菜鸡一枚~</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">19</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">13</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">14</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Pride-lee"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Pride-lee" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="http://wpa.qq.com/msgrd?v=3&uin=211977992&site=qq&menu=yes" target="_blank" title="qq"><i class="fa-brands fa-qq"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">99年程序猿,兴趣爱好多,代码、软件硬件、智能家居、穿越机甚至钓鱼,喜欢一切有意思的东西❤️🧡💛💚💙💜</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%95%B0%E6%8D%AE%E7%BB%91%E5%AE%9A"><span class="toc-number">1.</span> <span class="toc-text">小程序数据绑定</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89%E6%95%B0%E6%8D%AE"><span class="toc-number">1.1.</span> <span class="toc-text">定义数据</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A"><span class="toc-number">2.</span> <span class="toc-text">事件绑定</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8"><span class="toc-number">2.1.</span> <span class="toc-text">事件对象的属性列表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#target-%E5%92%8C-currentTarget%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">2.2.</span> <span class="toc-text">target 和 currentTarget的区别</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#bindtap%E7%9A%84%E8%AF%AD%E6%B3%95%E6%A0%BC%E5%BC%8F"><span class="toc-number">2.3.</span> <span class="toc-text">bindtap的语法格式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0%E4%B8%AD%E4%B8%BA-data-%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE%E8%B5%8B%E5%80%BC"><span class="toc-number">2.4.</span> <span class="toc-text">在事件处理函数中为 data 中的数据赋值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E4%BC%A0%E5%8F%82"><span class="toc-number">2.5.</span> <span class="toc-text">事件传参</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#bindinput%E8%AF%AD%E6%B3%95"><span class="toc-number">2.6.</span> <span class="toc-text">bindinput语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%87%E6%9C%AC%E6%A1%86%E5%92%8C-data-%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5"><span class="toc-number">2.7.</span> <span class="toc-text">文本框和 data 数据同步</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9D%A1%E4%BB%B6%E6%B8%B2%E6%9F%93"><span class="toc-number">3.</span> <span class="toc-text">条件渲染</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BB%93%E5%90%88-lt-block-gt-%E4%BD%BF%E7%94%A8-wx-if"><span class="toc-number">3.1.</span> <span class="toc-text">结合 < block > 使用 wx:if</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#hidden"><span class="toc-number">3.2.</span> <span class="toc-text">hidden</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#wx-if-%E4%B8%8E-hidden-%E7%9A%84%E5%AF%B9%E6%AF%94"><span class="toc-number">3.3.</span> <span class="toc-text">wx:if 与 hidden 的对比</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%97%E8%A1%A8%E6%B8%B2%E6%9F%93"><span class="toc-number">4.</span> <span class="toc-text">列表渲染</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#wx-for"><span class="toc-number">4.1.</span> <span class="toc-text">wx:for</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%8B%E5%8A%A8%E6%8C%87%E5%AE%9A%E7%B4%A2%E5%BC%95%E5%92%8C%E5%BD%93%E5%89%8D%E9%A1%B9%E7%9A%84%E5%8F%98%E9%87%8F%E5%90%8D"><span class="toc-number">4.2.</span> <span class="toc-text">手动指定索引和当前项的变量名</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#wx-key%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">4.3.</span> <span class="toc-text">wx:key的使用</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#WXSS"><span class="toc-number">5.</span> <span class="toc-text">WXSS</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#WXSS-%E5%92%8C-CSS%E7%9A%84%E5%85%B3%E7%B3%BB"><span class="toc-number">5.1.</span> <span class="toc-text">WXSS 和 CSS的关系</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E6%A0%B7%E5%BC%8F%E3%80%81%E5%B1%80%E9%83%A8%E6%A0%B7%E5%BC%8F"><span class="toc-number">5.2.</span> <span class="toc-text">全局样式、局部样式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E9%85%8D%E7%BD%AE"><span class="toc-number">6.</span> <span class="toc-text">全局配置</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">6.1.</span> <span class="toc-text">全局配置文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#window%E8%8A%82%E7%82%B9%E5%B8%B8%E7%94%A8%E9%85%8D%E7%BD%AE"><span class="toc-number">6.2.</span> <span class="toc-text">window节点常用配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A8%A1%E6%8B%9F%E5%99%A8%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-number">6.3.</span> <span class="toc-text">模拟器的问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%8A%E6%8B%89%E8%A7%A6%E5%BA%95%E7%9A%84%E8%B7%9D%E7%A6%BB"><span class="toc-number">6.4.</span> <span class="toc-text">上拉触底的距离</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#tabBar"><span class="toc-number">6.5.</span> <span class="toc-text">tabBar</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#tabBar%E7%9A%846%E4%B8%AA%E7%BB%84%E6%88%90%E9%83%A8%E5%88%86"><span class="toc-number">6.6.</span> <span class="toc-text">tabBar的6个组成部分</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#tabBar%E9%85%8D%E7%BD%AE"><span class="toc-number">6.7.</span> <span class="toc-text">tabBar配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E8%AF%B7%E6%B1%82"><span class="toc-number">7.</span> <span class="toc-text">数据请求</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%B8%AD%E7%9A%84%E9%99%90%E5%88%B6"><span class="toc-number">7.1.</span> <span class="toc-text">小程序中的限制</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%91get-x2F-post%E8%AF%B7%E6%B1%82"><span class="toc-number">7.2.</span> <span class="toc-text">发get/post请求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A1%B5%E9%9D%A2%E5%8A%A0%E8%BD%BD%E6%97%B6%E5%8F%91%E8%AF%B7%E6%B1%82"><span class="toc-number">7.3.</span> <span class="toc-text">页面加载时发请求</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/jd-yangmao.html" title="全自动薅JD羊毛"><img src="https://s1.ax1x.com/2022/07/05/jNqUyD.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="全自动薅JD羊毛"/></a><div class="content"><a class="title" href="/jd-yangmao.html" title="全自动薅JD羊毛">全自动薅JD羊毛</a><time datetime="2023-03-12T14:45:28.000Z" title="发表于 2023-03-12 22:45:28">2023-03-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/mini-program-2.html" title="微信小程序知识点(二)"><img src="https://s1.ax1x.com/2022/07/05/jNjg4s.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="微信小程序知识点(二)"/></a><div class="content"><a class="title" href="/mini-program-2.html" title="微信小程序知识点(二)">微信小程序知识点(二)</a><time datetime="2022-11-28T12:13:10.000Z" title="发表于 2022-11-28 20:13:10">2022-11-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/mini-program.html" title="微信小程序知识点(一)"><img src="https://s1.ax1x.com/2022/07/05/jNqUyD.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="微信小程序知识点(一)"/></a><div class="content"><a class="title" href="/mini-program.html" title="微信小程序知识点(一)">微信小程序知识点(一)</a><time datetime="2022-11-27T11:45:28.000Z" title="发表于 2022-11-27 19:45:28">2022-11-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/chrome-cache.html" title="浏览器的缓存机制"><img src="https://s1.ax1x.com/2022/07/25/jvWCsH.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="浏览器的缓存机制"/></a><div class="content"><a class="title" href="/chrome-cache.html" title="浏览器的缓存机制">浏览器的缓存机制</a><time datetime="2022-09-23T12:20:28.000Z" title="发表于 2022-09-23 20:20:28">2022-09-23</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/fe-vuex.html" title="什么是Vuex?"><img src="https://s1.ax1x.com/2022/07/05/jNjg4s.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="什么是Vuex?"/></a><div class="content"><a class="title" href="/fe-vuex.html" title="什么是Vuex?">什么是Vuex?</a><time datetime="2022-07-23T10:06:04.000Z" title="发表于 2022-07-23 18:06:04">2022-07-23</time></div></div></div></div></div></div></main><footer id="footer" style="background: rgb(252,118,153)radial-gradient(circle, rgba(252,118,153,1) 0%, rgba(139,216,238,1) 100%);"><div id="footer-wrap"><div class="copyright">©2020 - 2023 By Pride_Lee</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span> 数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="/js/search/local-search.js"></script><script>var preloader = {
endLoading: () => {
document.body.style.overflow = 'auto';
document.getElementById('loading-box').classList.add("loaded")
},
initLoading: () => {
document.body.style.overflow = '';
document.getElementById('loading-box').classList.remove("loaded")
}
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadValine () {
function initValine () {
const valine = new Valine(Object.assign({
el: '#vcomment',
appId: 'tLdhKVV1NoSACqDtL4PqycT5-gzGzoHsz',
appKey: 'V6GdVccl5jFRALt0jxwvRWNO',
avatar: 'monsterid',
serverURLs: '',
emojiMaps: "",
path: window.location.pathname,
visitor: false
}, null))
}
if (typeof Valine === 'function') initValine()
else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}
if ('Valine' === 'Valine' || !false) {
if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
else setTimeout(loadValine, 0)
} else {
function loadOtherComment () {
loadValine()
}
}</script><script>function loadLivere () {
if (typeof LivereTower === 'object') {
window.LivereTower.init()
}
else {
(function(d, s) {
var j, e = d.getElementsByTagName(s)[0];
if (typeof LivereTower === 'function') { return; }
j = d.createElement(s);
j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
j.async = true;
e.parentNode.insertBefore(j, e);
})(document, 'script');
}
}
if ('Valine' === 'Livere' || !false) {
if (false) btf.loadComment(document.getElementById('lv-container'), loadLivere)
else loadLivere()
}
else {
function loadOtherComment () {
loadLivere()
}
}</script></div><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>