QQ小程序 組件間關(guān)系

2020-07-02 09:33 更新

定義和使用組件間關(guān)系

  1. <custom-ul>
  2. <custom-li>item 1</custom-li>
  3. <custom-li>item 2</custom-li>
  4. </custom-ul>

這個例子中, custom-ulcustom-li 都是自定義組件,它們有相互間的關(guān)系,相互間的通信往往比較復(fù)雜。此時在組件定義時加入 relations 定義段,可以解決這樣的問題。示例:

  1. // path/to/custom-ul.js
  2. Component({
  3. relations: {
  4. './custom-li': {
  5. type: 'child', // 關(guān)聯(lián)的目標(biāo)節(jié)點應(yīng)為子節(jié)點
  6. linked(target) {
  7. // 每次有custom-li被插入時執(zhí)行,target是該節(jié)點實例對象,觸發(fā)在該節(jié)點attached生命周期之后
  8. },
  9. linkChanged(target) {
  10. // 每次有custom-li被移動后執(zhí)行,target是該節(jié)點實例對象,觸發(fā)在該節(jié)點moved生命周期之后
  11. },
  12. unlinked(target) {
  13. // 每次有custom-li被移除時執(zhí)行,target是該節(jié)點實例對象,觸發(fā)在該節(jié)點detached生命周期之后
  14. }
  15. }
  16. },
  17. methods: {
  18. _getAllLi() {
  19. // 使用getRelationNodes可以獲得nodes數(shù)組,包含所有已關(guān)聯(lián)的custom-li,且是有序的
  20. const nodes = this.getRelationNodes('path/to/custom-li')
  21. }
  22. },
  23. ready() {
  24. this._getAllLi()
  25. }
  26. })

  1. // path/to/custom-li.js
  2. Component({
  3. relations: {
  4. './custom-ul': {
  5. type: 'parent', // 關(guān)聯(lián)的目標(biāo)節(jié)點應(yīng)為父節(jié)點
  6. linked(target) {
  7. // 每次被插入到custom-ul時執(zhí)行,target是custom-ul節(jié)點實例對象,觸發(fā)在attached生命周期之后
  8. },
  9. linkChanged(target) {
  10. // 每次被移動后執(zhí)行,target是custom-ul節(jié)點實例對象,觸發(fā)在moved生命周期之后
  11. },
  12. unlinked(target) {
  13. // 每次被移除時執(zhí)行,target是custom-ul節(jié)點實例對象,觸發(fā)在detached生命周期之后
  14. }
  15. }
  16. }
  17. })

注意:必須在兩個組件定義中都加入relations定義,否則不會生效。

關(guān)聯(lián)一類組件

有時,需要關(guān)聯(lián)的是一類組件,如:

  1. <custom-form>
  2. <view>
  3. input
  4. <custom-input></custom-input>
  5. </view>
  6. <custom-submit>submit</custom-submit>
  7. </custom-form>

custom-form 組件想要關(guān)聯(lián) custom-inputcustom-submit 兩個組件。此時,如果這兩個組件都有同一個behavior:

  1. // path/to/custom-form-controls.js
  2. module.exports = Behavior({
  3. // ...
  4. })

  1. // path/to/custom-submit.js
  2. const customFormControls = require('./custom-form-controls')
  3. Component({
  4. behaviors: [customFormControls],
  5. relations: {
  6. './custom-form': {
  7. type: 'ancestor', // 關(guān)聯(lián)的目標(biāo)節(jié)點應(yīng)為祖先節(jié)點
  8. }
  9. }
  10. })

則在 relations 關(guān)系定義中,可使用這個behavior來代替組件路徑作為關(guān)聯(lián)的目標(biāo)節(jié)點:

  1. // path/to/custom-form.js
  2. const customFormControls = require('./custom-form-controls')
  3. Component({
  4. relations: {
  5. customFormControls: {
  6. type: 'descendant', // 關(guān)聯(lián)的目標(biāo)節(jié)點應(yīng)為子孫節(jié)點
  7. target: customFormControls
  8. }
  9. }
  10. })

relations 定義段

relations 定義段包含目標(biāo)組件路徑及其對應(yīng)選項,可包含的選項見下表。

選項 類型 是否必填 描述
type String 目標(biāo)組件的相對關(guān)系,可選的值為 parent 、 child 、 ancestor 、 descendant
linked Function 關(guān)系生命周期函數(shù),當(dāng)關(guān)系被建立在頁面節(jié)點樹中時觸發(fā),觸發(fā)時機在組件attached生命周期之后
linkChanged Function 關(guān)系生命周期函數(shù),當(dāng)關(guān)系在頁面節(jié)點樹中發(fā)生改變時觸發(fā),觸發(fā)時機在組件moved生命周期之后
unlinked Function 關(guān)系生命周期函數(shù),當(dāng)關(guān)系脫離頁面節(jié)點樹時觸發(fā),觸發(fā)時機在組件detached生命周期之后
target String 如果這一項被設(shè)置,則它表示關(guān)聯(lián)的目標(biāo)節(jié)點所應(yīng)具有的behavior,所有擁有這一behavior的組件節(jié)點都會被關(guān)聯(lián)
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號