微信小程序從入門到學會第七天-------小程序的自定義組件

一、前言

當我們多次使用同一個組件的時候懒闷,我們可以將其進行封裝然后進行使用十减,這個時候,自定義組件就起到了很好的作用愤估,前面雖然我們講到了自定義組件帮辟,不過那都是皮毛,僅僅只是講到了自定義組件的創(chuàng)建和使用罷了玩焰,下面我們來深入剖析自定義組件由驹。首先看下目錄:

  • 自定義組件的參數(shù)
  • 自定義組件的生命周期
  • 插槽

匿名插槽

具名插槽

外部樣式

多樣化的組件通信

1.雙向綁定

2.父子組件互相傳

組件共享

組件關系

二、自定義組件的參數(shù)

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

定義段

|

類型

|

是否必填

|

描述

|

最低版本

|
|

properties

|

Object Map

|

|

組件的對外屬性昔园,是屬性名到屬性設置的映射表

| |
|

data

|

Object

|

|

組件的內(nèi)部數(shù)據(jù)荔棉,和 properties一同用于組件的模板渲染

| |
|

observers

|

Object

|

|

組件數(shù)據(jù)字段監(jiān)聽器,用于監(jiān)聽 properties 和 data 的變化

|

2.6.1

|
|

methods

|

Object

|

|

組件的方法蒿赢,包括事件響應函數(shù)和任意的自定義方法秉继,關于事件響應函數(shù)的使用

| |
|

behaviors

|

String Array

|

|

類似于mixins和traits的組件間代碼復用機制

| |
|

created

|

Function

|

|

組件生命周期函數(shù)-在組件實例剛剛被創(chuàng)建時執(zhí)行舱痘,注意此時不能調(diào)用setData )

| |
|

attached

|

Function

|

|

組件生命周期函數(shù)-在組件實例進入頁面節(jié)點樹時執(zhí)行)

| |
|

ready

|

Function

|

|

組件生命周期函數(shù)-在組件布局完成后執(zhí)行)

| |
|

moved

|

Function

|

|

組件生命周期函數(shù)-在組件實例被移動到節(jié)點樹另一個位置時執(zhí)行)

| |
|

detached

|

Function

|

|

組件生命周期函數(shù)-在組件實例被從頁面節(jié)點樹移除時執(zhí)行)

| |
|

relations

|

Object

|

|

組件間關系

| |
|

externalClasses

|

String Array

|

|

組件接受的外部樣式類

| |
|

options

|

Object Map

|

|

選項

| |
|

lifetimes

|

Object

|

|

組件生命周期聲明對象

|

2.2.3

|
|

pageLifetimes

|

Object

|

|

組件所在頁面的生命周期聲明對象

|

2.2.3

|
|

definitionFilter

|

Function

|

|

定義段過濾器,用于自定義組件擴展

|

2.2.3

|

生成的組件實例可以在組件的方法、生命周期函數(shù)和屬性 observer 中通過 this訪問虑乖。組件還有一些通用屬性和方法粤剧,如下:

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

屬性名

|

類型

|

描述

|
|

is

|

String

|

組件的文件路徑

|
|

id

|

String

|

節(jié)點id

|
|

dataset

|

String

|

節(jié)點dataset

|
|

data

|

Object

|

組件數(shù)據(jù),包括內(nèi)部數(shù)據(jù)和屬性值

|
|

properties

|

Object

|

組件數(shù)據(jù),包括內(nèi)部數(shù)據(jù)和屬性值(與 data一致)

|
|

router

|

Object

|

相對于當前自定義組件的Router對象

|
|

pageRouter

|

Object

|

相對于當前自定義組件所在頁面的Router對象

|

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

方法名

|

參數(shù)

|

描述

|

最低版本

|
|

setData

|

Object newData

|

設置data并執(zhí)行視圖層渲染

| |
|

hasBehavior

|

Object behavior

|

檢查組件是否具有 behavior(檢查時會遞歸檢查被直接或間接引入的所有behavior)

| |
|

triggerEvent

|

String name, Object detail, Object options

|

觸發(fā)事件

| |
|

createSelectorQuery

| |

創(chuàng)建一個 SelectorQuery對象养篓,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createIntersectionObserver

| |

創(chuàng)建一個IntersectionObserver對象,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createMediaQueryObserver

| |

創(chuàng)建一個 MediaQueryObserver 對象

|

2.11.1

|
|

selectComponent

|

String selector

|

使用選擇器選擇組件實例節(jié)點赂蕴,返回匹配到的第一個組件實例對象(會被 wx://component-export 影響)

| |
|

selectAllComponents

|

String selector

|

使用選擇器選擇組件實例節(jié)點柳弄,返回匹配到的全部組件實例對象組成的數(shù)組(會被 wx://component-export 影響)

| |
|

selectOwnerComponent

| |

選取當前組件節(jié)點所在的組件實例(即組件的引用者),返回它的組件實例對象(會被 wx://component-export 影響)

|

2.8.2

|
|

getRelationNodes

|

String relationKey

|

獲取這個關系所對應的所有關聯(lián)節(jié)點

| |
|

groupSetData

|

Function callback

|

立刻執(zhí)行 callback 概说,其中的多個 setData之間不會觸發(fā)界面繪制(只有某些特殊場景中需要碧注,如用于在不同組件同時 setData時進行界面繪制同步)

|

2.4.0

|
|

getTabBar

| |

返回當前頁面的 custom-tab-bar 的組件實例

|

2.6.2

|
|

getPageId

| |

返回頁面標識符(一個字符串),可以用來判斷幾個自定義組件實例是不是在同一個頁面內(nèi)

|

2.7.1

|
|

animate

|

String selector, Array keyframes, Number duration, Function callback

|

執(zhí)行關鍵幀動畫

|

2.9.0

|
|

clearAnimation

|

String selector Object options, Function callback

|

清除關鍵幀動畫

|

2.9.0

|
|

setUpdatePerformanceListener

|

Object options Function listener

|

清除關鍵幀動畫

|

2.12.0

|

注:以上內(nèi)容來源于微信開放文檔二糖赔、自定義組件的參數(shù)

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

定義段

|

類型

|

是否必填

|

描述

|

最低版本

|
|

properties

|

Object Map

|

|

組件的對外屬性萍丐,是屬性名到屬性設置的映射表

| |
|

data

|

Object

|

|

組件的內(nèi)部數(shù)據(jù),和 properties一同用于組件的模板渲染

| |
|

observers

|

Object

|

|

組件數(shù)據(jù)字段監(jiān)聽器放典,用于監(jiān)聽 properties 和 data 的變化

|

2.6.1

|
|

methods

|

Object

|

|

組件的方法逝变,包括事件響應函數(shù)和任意的自定義方法,關于事件響應函數(shù)的使用

| |
|

behaviors

|

String Array

|

|

類似于mixins和traits的組件間代碼復用機制

| |
|

created

|

Function

|

|

組件生命周期函數(shù)-在組件實例剛剛被創(chuàng)建時執(zhí)行奋构,注意此時不能調(diào)用setData )

| |
|

attached

|

Function

|

|

組件生命周期函數(shù)-在組件實例進入頁面節(jié)點樹時執(zhí)行)

| |
|

ready

|

Function

|

|

組件生命周期函數(shù)-在組件布局完成后執(zhí)行)

| |
|

moved

|

Function

|

|

組件生命周期函數(shù)-在組件實例被移動到節(jié)點樹另一個位置時執(zhí)行)

| |
|

detached

|

Function

|

|

組件生命周期函數(shù)-在組件實例被從頁面節(jié)點樹移除時執(zhí)行)

| |
|

relations

|

Object

|

|

組件間關系

| |
|

externalClasses

|

String Array

|

|

組件接受的外部樣式類

| |
|

options

|

Object Map

|

|

選項

| |
|

lifetimes

|

Object

|

|

組件生命周期聲明對象

|

2.2.3

|
|

pageLifetimes

|

Object

|

|

組件所在頁面的生命周期聲明對象

|

2.2.3

|
|

definitionFilter

|

Function

|

|

定義段過濾器壳影,用于自定義組件擴展

|

2.2.3

|

生成的組件實例可以在組件的方法、生命周期函數(shù)和屬性 observer 中通過 this訪問弥臼。組件還有一些通用屬性和方法态贤,如下:

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

屬性名

|

類型

|

描述

|
|

is

|

String

|

組件的文件路徑

|
|

id

|

String

|

節(jié)點id

|
|

dataset

|

String

|

節(jié)點dataset

|
|

data

|

Object

|

組件數(shù)據(jù),包括內(nèi)部數(shù)據(jù)和屬性值

|
|

properties

|

Object

|

組件數(shù)據(jù)醋火,包括內(nèi)部數(shù)據(jù)和屬性值(與 data一致)

|
|

router

|

Object

|

相對于當前自定義組件的Router對象

|
|

pageRouter

|

Object

|

相對于當前自定義組件所在頁面的Router對象

|

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

方法名

|

參數(shù)

|

描述

|

最低版本

|
|

setData

|

Object newData

|

設置data并執(zhí)行視圖層渲染

| |
|

hasBehavior

|

Object behavior

|

檢查組件是否具有 behavior(檢查時會遞歸檢查被直接或間接引入的所有behavior)

| |
|

triggerEvent

|

String name, Object detail, Object options

|

觸發(fā)事件

| |
|

createSelectorQuery

| |

創(chuàng)建一個 SelectorQuery對象悠汽,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createIntersectionObserver

| |

創(chuàng)建一個IntersectionObserver對象,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createMediaQueryObserver

| |

創(chuàng)建一個 MediaQueryObserver 對象

|

2.11.1

|
|

selectComponent

|

String selector

|

使用選擇器選擇組件實例節(jié)點芥驳,返回匹配到的第一個組件實例對象(會被 wx://component-export 影響)

| |
|

selectAllComponents

|

String selector

|

使用選擇器選擇組件實例節(jié)點柿冲,返回匹配到的全部組件實例對象組成的數(shù)組(會被 wx://component-export 影響)

| |
|

selectOwnerComponent

| |

選取當前組件節(jié)點所在的組件實例(即組件的引用者),返回它的組件實例對象(會被 wx://component-export 影響)

|

2.8.2

|
|

getRelationNodes

|

String relationKey

|

獲取這個關系所對應的所有關聯(lián)節(jié)點

| |
|

groupSetData

|

Function callback

|

立刻執(zhí)行 callback 兆旬,其中的多個 setData之間不會觸發(fā)界面繪制(只有某些特殊場景中需要假抄,如用于在不同組件同時 setData時進行界面繪制同步)

|

2.4.0

|
|

getTabBar

| |

返回當前頁面的 custom-tab-bar 的組件實例

|

2.6.2

|
|

getPageId

| |

返回頁面標識符(一個字符串),可以用來判斷幾個自定義組件實例是不是在同一個頁面內(nèi)

|

2.7.1

|
|

animate

|

String selector, Array keyframes, Number duration, Function callback

|

執(zhí)行關鍵幀動畫

|

2.9.0

|
|

clearAnimation

|

String selector Object options, Function callback

|

清除關鍵幀動畫

|

2.9.0

|
|

setUpdatePerformanceListener

|

Object options Function listener

|

清除關鍵幀動畫

|

2.12.0

|

注:以上內(nèi)容來源于微信開放文檔二丽猬、自定義組件的參數(shù)

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

定義段

|

類型

|

是否必填

|

描述

|

最低版本

|
|

properties

|

Object Map

|

|

組件的對外屬性宿饱,是屬性名到屬性設置的映射表

| |
|

data

|

Object

|

|

組件的內(nèi)部數(shù)據(jù),和 properties一同用于組件的模板渲染

| |
|

observers

|

Object

|

|

組件數(shù)據(jù)字段監(jiān)聽器脚祟,用于監(jiān)聽 properties 和 data 的變化

|

2.6.1

|
|

methods

|

Object

|

|

組件的方法谬以,包括事件響應函數(shù)和任意的自定義方法,關于事件響應函數(shù)的使用

| |
|

behaviors

|

String Array

|

|

類似于mixins和traits的組件間代碼復用機制

| |
|

created

|

Function

|

|

組件生命周期函數(shù)-在組件實例剛剛被創(chuàng)建時執(zhí)行由桌,注意此時不能調(diào)用setData )

| |
|

attached

|

Function

|

|

組件生命周期函數(shù)-在組件實例進入頁面節(jié)點樹時執(zhí)行)

| |
|

ready

|

Function

|

|

組件生命周期函數(shù)-在組件布局完成后執(zhí)行)

| |
|

moved

|

Function

|

|

組件生命周期函數(shù)-在組件實例被移動到節(jié)點樹另一個位置時執(zhí)行)

| |
|

detached

|

Function

|

|

組件生命周期函數(shù)-在組件實例被從頁面節(jié)點樹移除時執(zhí)行)

| |
|

relations

|

Object

|

|

組件間關系

| |
|

externalClasses

|

String Array

|

|

組件接受的外部樣式類

| |
|

options

|

Object Map

|

|

選項

| |
|

lifetimes

|

Object

|

|

組件生命周期聲明對象

|

2.2.3

|
|

pageLifetimes

|

Object

|

|

組件所在頁面的生命周期聲明對象

|

2.2.3

|
|

definitionFilter

|

Function

|

|

定義段過濾器为黎,用于自定義組件擴展

|

2.2.3

|

生成的組件實例可以在組件的方法邮丰、生命周期函數(shù)和屬性 observer 中通過 this訪問。組件還有一些通用屬性和方法铭乾,如下:

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

屬性名

|

類型

|

描述

|
|

is

|

String

|

組件的文件路徑

|
|

id

|

String

|

節(jié)點id

|
|

dataset

|

String

|

節(jié)點dataset

|
|

data

|

Object

|

組件數(shù)據(jù)剪廉,包括內(nèi)部數(shù)據(jù)和屬性值

|
|

properties

|

Object

|

組件數(shù)據(jù),包括內(nèi)部數(shù)據(jù)和屬性值(與 data一致)

|
|

router

|

Object

|

相對于當前自定義組件的Router對象

|
|

pageRouter

|

Object

|

相對于當前自定義組件所在頁面的Router對象

|

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

方法名

|

參數(shù)

|

描述

|

最低版本

|
|

setData

|

Object newData

|

設置data并執(zhí)行視圖層渲染

| |
|

hasBehavior

|

Object behavior

|

檢查組件是否具有 behavior(檢查時會遞歸檢查被直接或間接引入的所有behavior)

| |
|

triggerEvent

|

String name, Object detail, Object options

|

觸發(fā)事件

| |
|

createSelectorQuery

| |

創(chuàng)建一個 SelectorQuery對象炕檩,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createIntersectionObserver

| |

創(chuàng)建一個IntersectionObserver對象斗蒋,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createMediaQueryObserver

| |

創(chuàng)建一個 MediaQueryObserver 對象

|

2.11.1

|
|

selectComponent

|

String selector

|

使用選擇器選擇組件實例節(jié)點,返回匹配到的第一個組件實例對象(會被 wx://component-export 影響)

| |
|

selectAllComponents

|

String selector

|

使用選擇器選擇組件實例節(jié)點笛质,返回匹配到的全部組件實例對象組成的數(shù)組(會被 wx://component-export 影響)

| |
|

selectOwnerComponent

| |

選取當前組件節(jié)點所在的組件實例(即組件的引用者)泉沾,返回它的組件實例對象(會被 wx://component-export 影響)

|

2.8.2

|
|

getRelationNodes

|

String relationKey

|

獲取這個關系所對應的所有關聯(lián)節(jié)點

| |
|

groupSetData

|

Function callback

|

立刻執(zhí)行 callback ,其中的多個 setData之間不會觸發(fā)界面繪制(只有某些特殊場景中需要经瓷,如用于在不同組件同時 setData時進行界面繪制同步)

|

2.4.0

|
|

getTabBar

| |

返回當前頁面的 custom-tab-bar 的組件實例

|

2.6.2

|
|

getPageId

| |

返回頁面標識符(一個字符串)爆哑,可以用來判斷幾個自定義組件實例是不是在同一個頁面內(nèi)

|

2.7.1

|
|

animate

|

String selector, Array keyframes, Number duration, Function callback

|

執(zhí)行關鍵幀動畫

|

2.9.0

|
|

clearAnimation

|

String selector Object options, Function callback

|

清除關鍵幀動畫

|

2.9.0

|
|

setUpdatePerformanceListener

|

Object options Function listener

|

清除關鍵幀動畫

|

2.12.0

|

注:以上內(nèi)容來源于微信開放文檔二洞难、自定義組件的參數(shù)

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

定義段

|

類型

|

是否必填

|

描述

|

最低版本

|
|

properties

|

Object Map

|

|

組件的對外屬性舆吮,是屬性名到屬性設置的映射表

| |
|

data

|

Object

|

|

組件的內(nèi)部數(shù)據(jù),和 properties一同用于組件的模板渲染

| |
|

observers

|

Object

|

|

組件數(shù)據(jù)字段監(jiān)聽器队贱,用于監(jiān)聽 properties 和 data 的變化

|

2.6.1

|
|

methods

|

Object

|

|

組件的方法色冀,包括事件響應函數(shù)和任意的自定義方法,關于事件響應函數(shù)的使用

| |
|

behaviors

|

String Array

|

|

類似于mixins和traits的組件間代碼復用機制

| |
|

created

|

Function

|

|

組件生命周期函數(shù)-在組件實例剛剛被創(chuàng)建時執(zhí)行柱嫌,注意此時不能調(diào)用setData )

| |
|

attached

|

Function

|

|

組件生命周期函數(shù)-在組件實例進入頁面節(jié)點樹時執(zhí)行)

| |
|

ready

|

Function

|

|

組件生命周期函數(shù)-在組件布局完成后執(zhí)行)

| |
|

moved

|

Function

|

|

組件生命周期函數(shù)-在組件實例被移動到節(jié)點樹另一個位置時執(zhí)行)

| |
|

detached

|

Function

|

|

組件生命周期函數(shù)-在組件實例被從頁面節(jié)點樹移除時執(zhí)行)

| |
|

relations

|

Object

|

|

組件間關系

| |
|

externalClasses

|

String Array

|

|

組件接受的外部樣式類

| |
|

options

|

Object Map

|

|

選項

| |
|

lifetimes

|

Object

|

|

組件生命周期聲明對象

|

2.2.3

|
|

pageLifetimes

|

Object

|

|

組件所在頁面的生命周期聲明對象

|

2.2.3

|
|

definitionFilter

|

Function

|

|

定義段過濾器锋恬,用于自定義組件擴展

|

2.2.3

|

生成的組件實例可以在組件的方法、生命周期函數(shù)和屬性 observer 中通過 this訪問编丘。組件還有一些通用屬性和方法与学,如下:

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

屬性名

|

類型

|

描述

|
|

is

|

String

|

組件的文件路徑

|
|

id

|

String

|

節(jié)點id

|
|

dataset

|

String

|

節(jié)點dataset

|
|

data

|

Object

|

組件數(shù)據(jù),包括內(nèi)部數(shù)據(jù)和屬性值

|
|

properties

|

Object

|

組件數(shù)據(jù)嘉抓,包括內(nèi)部數(shù)據(jù)和屬性值(與 data一致)

|
|

router

|

Object

|

相對于當前自定義組件的Router對象

|
|

pageRouter

|

Object

|

相對于當前自定義組件所在頁面的Router對象

|

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

方法名

|

參數(shù)

|

描述

|

最低版本

|
|

setData

|

Object newData

|

設置data并執(zhí)行視圖層渲染

| |
|

hasBehavior

|

Object behavior

|

檢查組件是否具有 behavior(檢查時會遞歸檢查被直接或間接引入的所有behavior)

| |
|

triggerEvent

|

String name, Object detail, Object options

|

觸發(fā)事件

| |
|

createSelectorQuery

| |

創(chuàng)建一個 SelectorQuery對象索守,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createIntersectionObserver

| |

創(chuàng)建一個IntersectionObserver對象,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createMediaQueryObserver

| |

創(chuàng)建一個 MediaQueryObserver 對象

|

2.11.1

|
|

selectComponent

|

String selector

|

使用選擇器選擇組件實例節(jié)點抑片,返回匹配到的第一個組件實例對象(會被 wx://component-export 影響)

| |
|

selectAllComponents

|

String selector

|

使用選擇器選擇組件實例節(jié)點卵佛,返回匹配到的全部組件實例對象組成的數(shù)組(會被 wx://component-export 影響)

| |
|

selectOwnerComponent

| |

選取當前組件節(jié)點所在的組件實例(即組件的引用者),返回它的組件實例對象(會被 wx://component-export 影響)

|

2.8.2

|
|

getRelationNodes

|

String relationKey

|

獲取這個關系所對應的所有關聯(lián)節(jié)點

| |
|

groupSetData

|

Function callback

|

立刻執(zhí)行 callback 敞斋,其中的多個 setData之間不會觸發(fā)界面繪制(只有某些特殊場景中需要截汪,如用于在不同組件同時 setData時進行界面繪制同步)

|

2.4.0

|
|

getTabBar

| |

返回當前頁面的 custom-tab-bar 的組件實例

|

2.6.2

|
|

getPageId

| |

返回頁面標識符(一個字符串),可以用來判斷幾個自定義組件實例是不是在同一個頁面內(nèi)

|

2.7.1

|
|

animate

|

String selector, Array keyframes, Number duration, Function callback

|

執(zhí)行關鍵幀動畫

|

2.9.0

|
|

clearAnimation

|

String selector Object options, Function callback

|

清除關鍵幀動畫

|

2.9.0

|
|

setUpdatePerformanceListener

|

Object options Function listener

|

清除關鍵幀動畫

|

2.12.0

|

注:以上內(nèi)容來源于微信開放文檔

[圖片上傳失敗...(image-c484df-1640833244173)]

三植捎、自定義組件的生命周期

組件也有許多生命周期函數(shù)衙解,如下:

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

生命周期

|

參數(shù)

|

描述

|

最低版本

|
|

created

|

|

在組件實例剛剛被創(chuàng)建時執(zhí)行

|

1.6.3

|
|

attached

|

|

在組件實例進入頁面節(jié)點樹時執(zhí)行

|

1.6.3

|
|

ready

|

|

在組件在視圖層布局完成后執(zhí)行

|

1.6.3

|
|

moved

|

|

在組件實例被移動到節(jié)點樹另一個位置時執(zhí)行

|

1.6.3

|
|

detached

|

|

在組件實例被從頁面節(jié)點樹移除時執(zhí)行

|

1.6.3

|
|

error

|

Object Error

|

每當組件方法拋出錯誤時執(zhí)行

|

2.4.1

|

這些生命周期函數(shù)可以在自定義組件的參數(shù)外,也可以在組件的lifetimes參數(shù)內(nèi)焰枢,這樣優(yōu)先級更高丢郊;還有一類是組件所在頁面生命周期函數(shù)盔沫,如下:

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

生命周期

|

參數(shù)

|

描述

|

最低版本

|
|

show

|

|

組件所在的頁面被展示時執(zhí)行

|

2.2.3

|
|

hide

|

|

組件所在的頁面被隱藏時執(zhí)行

|

2.2.3

|
|

resize

|

Object Size

|

組件所在的頁面尺寸變化時執(zhí)行

|

2.4.0

|

這類生命周期函數(shù)必須在參數(shù)pageLifetimes內(nèi)。

注:以上內(nèi)容來源于微信開放文檔

更多精彩好文敬請關注公眾號“簡易編程網(wǎng)”

一枫匾、前言

當我們多次使用同一個組件的時候架诞,我們可以將其進行封裝然后進行使用,這個時候干茉,自定義組件就起到了很好的作用谴忧,前面雖然我們講到了自定義組件,不過那都是皮毛角虫,僅僅只是講到了自定義組件的創(chuàng)建和使用罷了沾谓,下面我們來深入剖析自定義組件。首先看下目錄:

  • 自定義組件的參數(shù)
  • 自定義組件的生命周期
  • 插槽

匿名插槽

具名插槽

外部樣式

多樣化的組件通信

1.雙向綁定

2.父子組件互相傳

組件共享

組件關系

二戳鹅、自定義組件的參數(shù)

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

定義段

|

類型

|

是否必填

|

描述

|

最低版本

|
|

properties

|

Object Map

|

|

組件的對外屬性均驶,是屬性名到屬性設置的映射表

| |
|

data

|

Object

|

|

組件的內(nèi)部數(shù)據(jù),和 properties一同用于組件的模板渲染

| |
|

observers

|

Object

|

|

組件數(shù)據(jù)字段監(jiān)聽器枫虏,用于監(jiān)聽 properties 和 data 的變化

|

2.6.1

|
|

methods

|

Object

|

|

組件的方法妇穴,包括事件響應函數(shù)和任意的自定義方法,關于事件響應函數(shù)的使用

| |
|

behaviors

|

String Array

|

|

類似于mixins和traits的組件間代碼復用機制

| |
|

created

|

Function

|

|

組件生命周期函數(shù)-在組件實例剛剛被創(chuàng)建時執(zhí)行隶债,注意此時不能調(diào)用setData )

| |
|

attached

|

Function

|

|

組件生命周期函數(shù)-在組件實例進入頁面節(jié)點樹時執(zhí)行)

| |
|

ready

|

Function

|

|

組件生命周期函數(shù)-在組件布局完成后執(zhí)行)

| |
|

moved

|

Function

|

|

組件生命周期函數(shù)-在組件實例被移動到節(jié)點樹另一個位置時執(zhí)行)

| |
|

detached

|

Function

|

|

組件生命周期函數(shù)-在組件實例被從頁面節(jié)點樹移除時執(zhí)行)

| |
|

relations

|

Object

|

|

組件間關系

| |
|

externalClasses

|

String Array

|

|

組件接受的外部樣式類

| |
|

options

|

Object Map

|

|

選項

| |
|

lifetimes

|

Object

|

|

組件生命周期聲明對象

|

2.2.3

|
|

pageLifetimes

|

Object

|

|

組件所在頁面的生命周期聲明對象

|

2.2.3

|
|

definitionFilter

|

Function

|

|

定義段過濾器腾它,用于自定義組件擴展

|

2.2.3

|

生成的組件實例可以在組件的方法、生命周期函數(shù)和屬性 observer 中通過 this訪問死讹。組件還有一些通用屬性和方法瞒滴,如下:

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

屬性名

|

類型

|

描述

|
|

is

|

String

|

組件的文件路徑

|
|

id

|

String

|

節(jié)點id

|
|

dataset

|

String

|

節(jié)點dataset

|
|

data

|

Object

|

組件數(shù)據(jù),包括內(nèi)部數(shù)據(jù)和屬性值

|
|

properties

|

Object

|

組件數(shù)據(jù)赞警,包括內(nèi)部數(shù)據(jù)和屬性值(與 data一致)

|
|

router

|

Object

|

相對于當前自定義組件的Router對象

|
|

pageRouter

|

Object

|

相對于當前自定義組件所在頁面的Router對象

|

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

方法名

|

參數(shù)

|

描述

|

最低版本

|
|

setData

|

Object newData

|

設置data并執(zhí)行視圖層渲染

| |
|

hasBehavior

|

Object behavior

|

檢查組件是否具有 behavior(檢查時會遞歸檢查被直接或間接引入的所有behavior)

| |
|

triggerEvent

|

String name, Object detail, Object options

|

觸發(fā)事件

| |
|

createSelectorQuery

| |

創(chuàng)建一個 SelectorQuery對象妓忍,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createIntersectionObserver

| |

創(chuàng)建一個IntersectionObserver對象,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createMediaQueryObserver

| |

創(chuàng)建一個 MediaQueryObserver 對象

|

2.11.1

|
|

selectComponent

|

String selector

|

使用選擇器選擇組件實例節(jié)點愧旦,返回匹配到的第一個組件實例對象(會被 wx://component-export 影響)

| |
|

selectAllComponents

|

String selector

|

使用選擇器選擇組件實例節(jié)點世剖,返回匹配到的全部組件實例對象組成的數(shù)組(會被 wx://component-export 影響)

| |
|

selectOwnerComponent

| |

選取當前組件節(jié)點所在的組件實例(即組件的引用者),返回它的組件實例對象(會被 wx://component-export 影響)

|

2.8.2

|
|

getRelationNodes

|

String relationKey

|

獲取這個關系所對應的所有關聯(lián)節(jié)點

| |
|

groupSetData

|

Function callback

|

立刻執(zhí)行 callback 忘瓦,其中的多個 setData之間不會觸發(fā)界面繪制(只有某些特殊場景中需要搁廓,如用于在不同組件同時 setData時進行界面繪制同步)

|

2.4.0

|
|

getTabBar

| |

返回當前頁面的 custom-tab-bar 的組件實例

|

2.6.2

|
|

getPageId

| |

返回頁面標識符(一個字符串),可以用來判斷幾個自定義組件實例是不是在同一個頁面內(nèi)

|

2.7.1

|
|

animate

|

String selector, Array keyframes, Number duration, Function callback

|

執(zhí)行關鍵幀動畫

|

2.9.0

|
|

clearAnimation

|

String selector Object options, Function callback

|

清除關鍵幀動畫

|

2.9.0

|
|

setUpdatePerformanceListener

|

Object options Function listener

|

清除關鍵幀動畫

|

2.12.0

|

注:以上內(nèi)容來源于微信開放文檔二耕皮、自定義組件的參數(shù)

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

定義段

|

類型

|

是否必填

|

描述

|

最低版本

|
|

properties

|

Object Map

|

|

組件的對外屬性境蜕,是屬性名到屬性設置的映射表

| |
|

data

|

Object

|

|

組件的內(nèi)部數(shù)據(jù),和 properties一同用于組件的模板渲染

| |
|

observers

|

Object

|

|

組件數(shù)據(jù)字段監(jiān)聽器凌停,用于監(jiān)聽 properties 和 data 的變化

|

2.6.1

|
|

methods

|

Object

|

|

組件的方法粱年,包括事件響應函數(shù)和任意的自定義方法,關于事件響應函數(shù)的使用

| |
|

behaviors

|

String Array

|

|

類似于mixins和traits的組件間代碼復用機制

| |
|

created

|

Function

|

|

組件生命周期函數(shù)-在組件實例剛剛被創(chuàng)建時執(zhí)行罚拟,注意此時不能調(diào)用setData )

| |
|

attached

|

Function

|

|

組件生命周期函數(shù)-在組件實例進入頁面節(jié)點樹時執(zhí)行)

| |
|

ready

|

Function

|

|

組件生命周期函數(shù)-在組件布局完成后執(zhí)行)

| |
|

moved

|

Function

|

|

組件生命周期函數(shù)-在組件實例被移動到節(jié)點樹另一個位置時執(zhí)行)

| |
|

detached

|

Function

|

|

組件生命周期函數(shù)-在組件實例被從頁面節(jié)點樹移除時執(zhí)行)

| |
|

relations

|

Object

|

|

組件間關系

| |
|

externalClasses

|

String Array

|

|

組件接受的外部樣式類

| |
|

options

|

Object Map

|

|

選項

| |
|

lifetimes

|

Object

|

|

組件生命周期聲明對象

|

2.2.3

|
|

pageLifetimes

|

Object

|

|

組件所在頁面的生命周期聲明對象

|

2.2.3

|
|

definitionFilter

|

Function

|

|

定義段過濾器台诗,用于自定義組件擴展

|

2.2.3

|

生成的組件實例可以在組件的方法完箩、生命周期函數(shù)和屬性 observer 中通過 this訪問。組件還有一些通用屬性和方法拉队,如下:

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

屬性名

|

類型

|

描述

|
|

is

|

String

|

組件的文件路徑

|
|

id

|

String

|

節(jié)點id

|
|

dataset

|

String

|

節(jié)點dataset

|
|

data

|

Object

|

組件數(shù)據(jù)弊知,包括內(nèi)部數(shù)據(jù)和屬性值

|
|

properties

|

Object

|

組件數(shù)據(jù),包括內(nèi)部數(shù)據(jù)和屬性值(與 data一致)

|
|

router

|

Object

|

相對于當前自定義組件的Router對象

|
|

pageRouter

|

Object

|

相對于當前自定義組件所在頁面的Router對象

|

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

方法名

|

參數(shù)

|

描述

|

最低版本

|
|

setData

|

Object newData

|

設置data并執(zhí)行視圖層渲染

| |
|

hasBehavior

|

Object behavior

|

檢查組件是否具有 behavior(檢查時會遞歸檢查被直接或間接引入的所有behavior)

| |
|

triggerEvent

|

String name, Object detail, Object options

|

觸發(fā)事件

| |
|

createSelectorQuery

| |

創(chuàng)建一個 SelectorQuery對象粱快,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createIntersectionObserver

| |

創(chuàng)建一個IntersectionObserver對象秩彤,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createMediaQueryObserver

| |

創(chuàng)建一個 MediaQueryObserver 對象

|

2.11.1

|
|

selectComponent

|

String selector

|

使用選擇器選擇組件實例節(jié)點,返回匹配到的第一個組件實例對象(會被 wx://component-export 影響)

| |
|

selectAllComponents

|

String selector

|

使用選擇器選擇組件實例節(jié)點事哭,返回匹配到的全部組件實例對象組成的數(shù)組(會被 wx://component-export 影響)

| |
|

selectOwnerComponent

| |

選取當前組件節(jié)點所在的組件實例(即組件的引用者)漫雷,返回它的組件實例對象(會被 wx://component-export 影響)

|

2.8.2

|
|

getRelationNodes

|

String relationKey

|

獲取這個關系所對應的所有關聯(lián)節(jié)點

| |
|

groupSetData

|

Function callback

|

立刻執(zhí)行 callback ,其中的多個 setData之間不會觸發(fā)界面繪制(只有某些特殊場景中需要鳍咱,如用于在不同組件同時 setData時進行界面繪制同步)

|

2.4.0

|
|

getTabBar

| |

返回當前頁面的 custom-tab-bar 的組件實例

|

2.6.2

|
|

getPageId

| |

返回頁面標識符(一個字符串)降盹,可以用來判斷幾個自定義組件實例是不是在同一個頁面內(nèi)

|

2.7.1

|
|

animate

|

String selector, Array keyframes, Number duration, Function callback

|

執(zhí)行關鍵幀動畫

|

2.9.0

|
|

clearAnimation

|

String selector Object options, Function callback

|

清除關鍵幀動畫

|

2.9.0

|
|

setUpdatePerformanceListener

|

Object options Function listener

|

清除關鍵幀動畫

|

2.12.0

|

注:以上內(nèi)容來源于微信開放文檔二、自定義組件的參數(shù)

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

定義段

|

類型

|

是否必填

|

描述

|

最低版本

|
|

properties

|

Object Map

|

|

組件的對外屬性谤辜,是屬性名到屬性設置的映射表

| |
|

data

|

Object

|

|

組件的內(nèi)部數(shù)據(jù)蓄坏,和 properties一同用于組件的模板渲染

| |
|

observers

|

Object

|

|

組件數(shù)據(jù)字段監(jiān)聽器,用于監(jiān)聽 properties 和 data 的變化

|

2.6.1

|
|

methods

|

Object

|

|

組件的方法每辟,包括事件響應函數(shù)和任意的自定義方法剑辫,關于事件響應函數(shù)的使用

| |
|

behaviors

|

String Array

|

|

類似于mixins和traits的組件間代碼復用機制

| |
|

created

|

Function

|

|

組件生命周期函數(shù)-在組件實例剛剛被創(chuàng)建時執(zhí)行干旧,注意此時不能調(diào)用setData )

| |
|

attached

|

Function

|

|

組件生命周期函數(shù)-在組件實例進入頁面節(jié)點樹時執(zhí)行)

| |
|

ready

|

Function

|

|

組件生命周期函數(shù)-在組件布局完成后執(zhí)行)

| |
|

moved

|

Function

|

|

組件生命周期函數(shù)-在組件實例被移動到節(jié)點樹另一個位置時執(zhí)行)

| |
|

detached

|

Function

|

|

組件生命周期函數(shù)-在組件實例被從頁面節(jié)點樹移除時執(zhí)行)

| |
|

relations

|

Object

|

|

組件間關系

| |
|

externalClasses

|

String Array

|

|

組件接受的外部樣式類

| |
|

options

|

Object Map

|

|

選項

| |
|

lifetimes

|

Object

|

|

組件生命周期聲明對象

|

2.2.3

|
|

pageLifetimes

|

Object

|

|

組件所在頁面的生命周期聲明對象

|

2.2.3

|
|

definitionFilter

|

Function

|

|

定義段過濾器渠欺,用于自定義組件擴展

|

2.2.3

|

生成的組件實例可以在組件的方法、生命周期函數(shù)和屬性 observer 中通過 this訪問椎眯。組件還有一些通用屬性和方法挠将,如下:

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

屬性名

|

類型

|

描述

|
|

is

|

String

|

組件的文件路徑

|
|

id

|

String

|

節(jié)點id

|
|

dataset

|

String

|

節(jié)點dataset

|
|

data

|

Object

|

組件數(shù)據(jù),包括內(nèi)部數(shù)據(jù)和屬性值

|
|

properties

|

Object

|

組件數(shù)據(jù)编整,包括內(nèi)部數(shù)據(jù)和屬性值(與 data一致)

|
|

router

|

Object

|

相對于當前自定義組件的Router對象

|
|

pageRouter

|

Object

|

相對于當前自定義組件所在頁面的Router對象

|

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

方法名

|

參數(shù)

|

描述

|

最低版本

|
|

setData

|

Object newData

|

設置data并執(zhí)行視圖層渲染

| |
|

hasBehavior

|

Object behavior

|

檢查組件是否具有 behavior(檢查時會遞歸檢查被直接或間接引入的所有behavior)

| |
|

triggerEvent

|

String name, Object detail, Object options

|

觸發(fā)事件

| |
|

createSelectorQuery

| |

創(chuàng)建一個 SelectorQuery對象舔稀,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createIntersectionObserver

| |

創(chuàng)建一個IntersectionObserver對象,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createMediaQueryObserver

| |

創(chuàng)建一個 MediaQueryObserver 對象

|

2.11.1

|
|

selectComponent

|

String selector

|

使用選擇器選擇組件實例節(jié)點掌测,返回匹配到的第一個組件實例對象(會被 wx://component-export 影響)

| |
|

selectAllComponents

|

String selector

|

使用選擇器選擇組件實例節(jié)點内贮,返回匹配到的全部組件實例對象組成的數(shù)組(會被 wx://component-export 影響)

| |
|

selectOwnerComponent

| |

選取當前組件節(jié)點所在的組件實例(即組件的引用者),返回它的組件實例對象(會被 wx://component-export 影響)

|

2.8.2

|
|

getRelationNodes

|

String relationKey

|

獲取這個關系所對應的所有關聯(lián)節(jié)點

| |
|

groupSetData

|

Function callback

|

立刻執(zhí)行 callback 汞斧,其中的多個 setData之間不會觸發(fā)界面繪制(只有某些特殊場景中需要夜郁,如用于在不同組件同時 setData時進行界面繪制同步)

|

2.4.0

|
|

getTabBar

| |

返回當前頁面的 custom-tab-bar 的組件實例

|

2.6.2

|
|

getPageId

| |

返回頁面標識符(一個字符串),可以用來判斷幾個自定義組件實例是不是在同一個頁面內(nèi)

|

2.7.1

|
|

animate

|

String selector, Array keyframes, Number duration, Function callback

|

執(zhí)行關鍵幀動畫

|

2.9.0

|
|

clearAnimation

|

String selector Object options, Function callback

|

清除關鍵幀動畫

|

2.9.0

|
|

setUpdatePerformanceListener

|

Object options Function listener

|

清除關鍵幀動畫

|

2.12.0

|

注:以上內(nèi)容來源于微信開放文檔二粘勒、自定義組件的參數(shù)

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

定義段

|

類型

|

是否必填

|

描述

|

最低版本

|
|

properties

|

Object Map

|

|

組件的對外屬性竞端,是屬性名到屬性設置的映射表

| |
|

data

|

Object

|

|

組件的內(nèi)部數(shù)據(jù),和 properties一同用于組件的模板渲染

| |
|

observers

|

Object

|

|

組件數(shù)據(jù)字段監(jiān)聽器庙睡,用于監(jiān)聽 properties 和 data 的變化

|

2.6.1

|
|

methods

|

Object

|

|

組件的方法事富,包括事件響應函數(shù)和任意的自定義方法技俐,關于事件響應函數(shù)的使用

| |
|

behaviors

|

String Array

|

|

類似于mixins和traits的組件間代碼復用機制

| |
|

created

|

Function

|

|

組件生命周期函數(shù)-在組件實例剛剛被創(chuàng)建時執(zhí)行,注意此時不能調(diào)用setData )

| |
|

attached

|

Function

|

|

組件生命周期函數(shù)-在組件實例進入頁面節(jié)點樹時執(zhí)行)

| |
|

ready

|

Function

|

|

組件生命周期函數(shù)-在組件布局完成后執(zhí)行)

| |
|

moved

|

Function

|

|

組件生命周期函數(shù)-在組件實例被移動到節(jié)點樹另一個位置時執(zhí)行)

| |
|

detached

|

Function

|

|

組件生命周期函數(shù)-在組件實例被從頁面節(jié)點樹移除時執(zhí)行)

| |
|

relations

|

Object

|

|

組件間關系

| |
|

externalClasses

|

String Array

|

|

組件接受的外部樣式類

| |
|

options

|

Object Map

|

|

選項

| |
|

lifetimes

|

Object

|

|

組件生命周期聲明對象

|

2.2.3

|
|

pageLifetimes

|

Object

|

|

組件所在頁面的生命周期聲明對象

|

2.2.3

|
|

definitionFilter

|

Function

|

|

定義段過濾器统台,用于自定義組件擴展

|

2.2.3

|

生成的組件實例可以在組件的方法雕擂、生命周期函數(shù)和屬性 observer 中通過 this訪問。組件還有一些通用屬性和方法,如下:

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

屬性名

|

類型

|

描述

|
|

is

|

String

|

組件的文件路徑

|
|

id

|

String

|

節(jié)點id

|
|

dataset

|

String

|

節(jié)點dataset

|
|

data

|

Object

|

組件數(shù)據(jù)厚宰,包括內(nèi)部數(shù)據(jù)和屬性值

|
|

properties

|

Object

|

組件數(shù)據(jù)霎烙,包括內(nèi)部數(shù)據(jù)和屬性值(與 data一致)

|
|

router

|

Object

|

相對于當前自定義組件的Router對象

|
|

pageRouter

|

Object

|

相對于當前自定義組件所在頁面的Router對象

|

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

方法名

|

參數(shù)

|

描述

|

最低版本

|
|

setData

|

Object newData

|

設置data并執(zhí)行視圖層渲染

| |
|

hasBehavior

|

Object behavior

|

檢查組件是否具有 behavior(檢查時會遞歸檢查被直接或間接引入的所有behavior)

| |
|

triggerEvent

|

String name, Object detail, Object options

|

觸發(fā)事件

| |
|

createSelectorQuery

| |

創(chuàng)建一個 SelectorQuery對象,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createIntersectionObserver

| |

創(chuàng)建一個IntersectionObserver對象族展,選擇器選取范圍為這個組件實例內(nèi)

| |
|

createMediaQueryObserver

| |

創(chuàng)建一個 MediaQueryObserver 對象

|

2.11.1

|
|

selectComponent

|

String selector

|

使用選擇器選擇組件實例節(jié)點,返回匹配到的第一個組件實例對象(會被 wx://component-export 影響)

| |
|

selectAllComponents

|

String selector

|

使用選擇器選擇組件實例節(jié)點拔鹰,返回匹配到的全部組件實例對象組成的數(shù)組(會被 wx://component-export 影響)

| |
|

selectOwnerComponent

| |

選取當前組件節(jié)點所在的組件實例(即組件的引用者)仪缸,返回它的組件實例對象(會被 wx://component-export 影響)

|

2.8.2

|
|

getRelationNodes

|

String relationKey

|

獲取這個關系所對應的所有關聯(lián)節(jié)點

| |
|

groupSetData

|

Function callback

|

立刻執(zhí)行 callback ,其中的多個 setData之間不會觸發(fā)界面繪制(只有某些特殊場景中需要列肢,如用于在不同組件同時 setData時進行界面繪制同步)

|

2.4.0

|
|

getTabBar

| |

返回當前頁面的 custom-tab-bar 的組件實例

|

2.6.2

|
|

getPageId

| |

返回頁面標識符(一個字符串)恰画,可以用來判斷幾個自定義組件實例是不是在同一個頁面內(nèi)

|

2.7.1

|
|

animate

|

String selector, Array keyframes, Number duration, Function callback

|

執(zhí)行關鍵幀動畫

|

2.9.0

|
|

clearAnimation

|

String selector Object options, Function callback

|

清除關鍵幀動畫

|

2.9.0

|
|

setUpdatePerformanceListener

|

Object options Function listener

|

清除關鍵幀動畫

|

2.12.0

|

注:以上內(nèi)容來源于微信開放文檔

[圖片上傳失敗...(image-916904-1640833287508)]

三、自定義組件的生命周期

組件也有許多生命周期函數(shù)瓷马,如下:

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

生命周期

|

參數(shù)

|

描述

|

最低版本

|
|

created

|

|

在組件實例剛剛被創(chuàng)建時執(zhí)行

|

1.6.3

|
|

attached

|

|

在組件實例進入頁面節(jié)點樹時執(zhí)行

|

1.6.3

|
|

ready

|

|

在組件在視圖層布局完成后執(zhí)行

|

1.6.3

|
|

moved

|

|

在組件實例被移動到節(jié)點樹另一個位置時執(zhí)行

|

1.6.3

|
|

detached

|

|

在組件實例被從頁面節(jié)點樹移除時執(zhí)行

|

1.6.3

|
|

error

|

Object Error

|

每當組件方法拋出錯誤時執(zhí)行

|

2.4.1

|

這些生命周期函數(shù)可以在自定義組件的參數(shù)外拴还,也可以在組件的lifetimes參數(shù)內(nèi),這樣優(yōu)先級更高欧聘;還有一類是組件所在頁面生命周期函數(shù)片林,如下:

<colgroup style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"><col style="user-select: text !important; box-sizing: border-box; -webkit-text-stroke: 0px !important;"></colgroup>
|

生命周期

|

參數(shù)

|

描述

|

最低版本

|
|

show

|

|

組件所在的頁面被展示時執(zhí)行

|

2.2.3

|
|

hide

|

|

組件所在的頁面被隱藏時執(zhí)行

|

2.2.3

|
|

resize

|

Object Size

|

組件所在的頁面尺寸變化時執(zhí)行

|

2.4.0

|

這類生命周期函數(shù)必須在參數(shù)pageLifetimes內(nèi)。

注:以上內(nèi)容來源于微信開放文檔

更多精彩好文敬請關注公眾號“簡易編程網(wǎng)”

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末怀骤,一起剝皮案震驚了整個濱河市费封,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蒋伦,老刑警劉巖弓摘,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異痕届,居然都是意外死亡韧献,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進店門研叫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來锤窑,“玉大人,你說我怎么就攤上這事蓝撇」矗” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵渤昌,是天一觀的道長虽抄。 經(jīng)常有香客問我走搁,道長,這世上最難降的妖魔是什么迈窟? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任私植,我火速辦了婚禮,結(jié)果婚禮上车酣,老公的妹妹穿的比我還像新娘曲稼。我一直安慰自己,他們只是感情好湖员,可當我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布贫悄。 她就那樣靜靜地躺著,像睡著了一般娘摔。 火紅的嫁衣襯著肌膚如雪窄坦。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天凳寺,我揣著相機與錄音鸭津,去河邊找鬼。 笑死肠缨,一個胖子當著我的面吹牛逆趋,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播晒奕,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼闻书,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了吴汪?” 一聲冷哼從身側(cè)響起惠窄,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤蒸眠,失蹤者是張志新(化名)和其女友劉穎漾橙,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體楞卡,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡霜运,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蒋腮。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片淘捡。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖池摧,靈堂內(nèi)的尸體忽然破棺而出焦除,到底是詐尸還是另有隱情,我是刑警寧澤作彤,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布膘魄,位于F島的核電站乌逐,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏创葡。R本人自食惡果不足惜浙踢,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望灿渴。 院中可真熱鬧洛波,春花似錦、人聲如沸骚露。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽棘幸。三九已至闻伶,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間够话,已是汗流浹背蓝翰。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留女嘲,地道東北人畜份。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像欣尼,于是被迫代替她去往敵國和親爆雹。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容