一、前言
當我們多次使用同一個組件的時候懒闷,我們可以將其進行封裝然后進行使用十减,這個時候,自定義組件就起到了很好的作用愤估,前面雖然我們講到了自定義組件帮辟,不過那都是皮毛,僅僅只是講到了自定義組件的創(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)”