Skip to content

Commit a7b7361

Browse files
committed
组件化开发
1 parent cc4f736 commit a7b7361

File tree

16 files changed

+249
-2
lines changed

16 files changed

+249
-2
lines changed

app.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
App({
2+
3+
/**
4+
* 当小程序初始化完成时,会触发 onLaunch(全局只触发一次)
5+
*/
6+
onLaunch: function () {
7+
8+
},
9+
10+
/**
11+
* 当小程序启动,或从后台进入前台显示,会触发 onShow
12+
*/
13+
onShow: function (options) {
14+
15+
},
16+
17+
/**
18+
* 当小程序从前台进入后台,会触发 onHide
19+
*/
20+
onHide: function () {
21+
22+
},
23+
24+
/**
25+
* 当小程序发生脚本错误,或者 api 调用失败时,会触发 onError 并带上错误信息
26+
*/
27+
onError: function (msg) {
28+
29+
}
30+
})

components/my-mslot/my-mslot.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// components/my-mslot/my-mslot.js
2+
Component({
3+
// ----------- 让使用者可以给组件传入数据 --------------
4+
properties: {
5+
title: {
6+
type: String,
7+
value: '',
8+
observer: function(newVal, oldVal) {
9+
10+
}
11+
}
12+
},
13+
14+
15+
// ----------- 定义组件内部的初始化数据 --------------
16+
data: {
17+
counter: 0
18+
},
19+
20+
21+
// ----------- 用于定义组件内部的函数 --------------
22+
methods: {
23+
foo() {
24+
25+
}
26+
},
27+
28+
29+
// ----------- 定义组件的配置选项 --------------
30+
// multipleSlots: 在使用多插槽时需要设置true
31+
// styleIsolation: 设置样式的隔离方式
32+
options: {
33+
multipleSlots: true
34+
},
35+
36+
37+
// ----------- 外界给组件传入额外的样式 --------------
38+
externalClasses: [],
39+
40+
41+
// ----------- 可以监听properties/data的改变 --------------
42+
observers: {
43+
counter: function(newVal) {
44+
console.log(newVal)
45+
}
46+
},
47+
48+
// ----------- 组件中监听生命周期函数 --------------
49+
// 1.监听所在页面的生命周期
50+
pageLifetimes: {
51+
show() {
52+
console.log('监听组件所在页面显示出来时')
53+
},
54+
hide() {
55+
console.log('监听组件所在页面隐藏起来时')
56+
},
57+
resize() {
58+
console.log('监听页面尺寸的改变')
59+
}
60+
},
61+
62+
// 2.监听组件本身的生命周期
63+
lifetimes: {
64+
created() {
65+
console.log('组件被创建出来')
66+
},
67+
attached() {
68+
console.log('组件被添加到页面')
69+
},
70+
ready() {
71+
console.log('组件被渲染出来')
72+
},
73+
moved() {
74+
console.log('组件被移动到另外一个节点')
75+
},
76+
detached() {
77+
console.log('组件被移除掉')
78+
}
79+
}
80+
})

components/my-mslot/my-mslot.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"component": true,
3+
"usingComponents": {}
4+
}

components/my-mslot/my-mslot.wxml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--components/my-mslot/my-mslot.wxml-->
2+
<view>我是mslot组件的开始</view>
3+
<view class='slot1'><slot name="slot1"/></view>
4+
<view class='slot2'><slot name="slot2"/></view>
5+
<view class='slot3'><slot name="slot3"/></view>
6+
<view>我是mslot组件的结尾</view>

components/my-mslot/my-mslot.wxss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* components/my-mslot/my-mslot.wxss */

components/my-sel/my-sel.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// components/my-sel/my-sel.js
2+
Component({
3+
/**
4+
* 组件的属性列表
5+
*/
6+
properties: {
7+
},
8+
9+
/**
10+
* 组件的初始数据
11+
*/
12+
data: {
13+
counter: 0
14+
},
15+
16+
/**
17+
* 组件的方法列表
18+
*/
19+
methods: {
20+
incrementCounter(num) {
21+
this.setData({
22+
counter: this.data.counter + num
23+
})
24+
}
25+
}
26+
})

components/my-sel/my-sel.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"component": true,
3+
"usingComponents": {}
4+
}

components/my-sel/my-sel.wxml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!--components/my-sel/my-sel.wxml-->
2+
<view>组件内的计数: {{counter}}</view>

components/my-sel/my-sel.wxss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* components/my-sel/my-sel.wxss */

components/my-slot/my-slot.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// components/my-slot/my-slot.js
2+
Component({
3+
/**
4+
* 组件的属性列表
5+
*/
6+
properties: {
7+
8+
},
9+
10+
/**
11+
* 组件的初始数据
12+
*/
13+
data: {
14+
15+
},
16+
17+
/**
18+
* 组件的方法列表
19+
*/
20+
methods: {
21+
22+
}
23+
})

0 commit comments

Comments
 (0)