字節(jié)跳動(dòng)小程序開(kāi)發(fā)框架 組件間關(guān)系

2019-08-07 09:32 更新

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

有時(shí)需要實(shí)現(xiàn)這樣的功能:

<custom-ul>
  <custom-li>item 1</custom-li>
  <custom-li>item 2</custom-li>
</custom-ul>

多層級(jí)自定義組件需要互相通信,實(shí)現(xiàn)起來(lái)比較復(fù)雜,這個(gè)時(shí)候可以使用 relations 字段解決。

// path/to/custom-ul.js
Component({
  relations: {
    './custom-li': {
      type: 'child', // 關(guān)聯(lián)的目標(biāo)節(jié)點(diǎn)應(yīng)為子節(jié)點(diǎn)
      linked(target) {
        // 每次有custom-li被插入時(shí)執(zhí)行,target是該節(jié)點(diǎn)實(shí)例對(duì)象,觸發(fā)在該節(jié)點(diǎn)attached生命周期之后
      },
      linkChanged(target) {
        // 每次有custom-li被移動(dòng)后執(zhí)行,target是該節(jié)點(diǎn)實(shí)例對(duì)象,觸發(fā)在該節(jié)點(diǎn)moved生命周期之后
      },
      unlinked(target) {
        // 每次有custom-li被移除時(shí)執(zhí)行,target是該節(jié)點(diǎn)實(shí)例對(duì)象,觸發(fā)在該節(jié)點(diǎn)detached生命周期之后
      }
    }
  },
  methods: {
    _getAllLi() {
      // 使用getRelationNodes可以獲得nodes數(shù)組,包含所有已關(guān)聯(lián)的custom-li,且是有序的
      this.getRelationNodes('path/to/custom-li', (nodes) => {
          // do something
      })
    }
  },
  ready() {
    this._getAllLi()
  }
})
// path/to/custom-li.js
Component({
  relations: {
    './custom-ul': {
      type: 'parent', // 關(guān)聯(lián)的目標(biāo)節(jié)點(diǎn)應(yīng)為父節(jié)點(diǎn)
      linked(target) {
        // 每次被插入到custom-ul時(shí)執(zhí)行,target是custom-ul節(jié)點(diǎn)實(shí)例對(duì)象,觸發(fā)在attached生命周期之后
      },
      linkChanged(target) {
        // 每次被移動(dòng)后執(zhí)行,target是custom-ul節(jié)點(diǎn)實(shí)例對(duì)象,觸發(fā)在moved生命周期之后
      },
      unlinked(target) {
        // 每次被移除時(shí)執(zhí)行,target是custom-ul節(jié)點(diǎn)實(shí)例對(duì)象,觸發(fā)在detached生命周期之后
      }
    }
  }
})

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

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

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

<custom-form>
  <view>
    <custom-input></custom-input>
  </view>
  <custom-submit>submit</custom-submit>
</custom-form>

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

// path/to/custom-form-controls.js
module.exports = Behavior({
  // ...
})
// path/to/custom-input.js
const customFormControls = require('./custom-form-controls')
Component({
  behaviors: [customFormControls],
  relations: {
    './custom-form': {
      type: 'ancestor', // 關(guān)聯(lián)的目標(biāo)節(jié)點(diǎn)應(yīng)為祖先節(jié)點(diǎn)
    }
  }
})
// path/to/custom-submit.js
const customFormControls = require('./custom-form-controls')
Component({
  behaviors: [customFormControls],
  relations: {
    './custom-form': {
      type: 'ancestor', // 關(guān)聯(lián)的目標(biāo)節(jié)點(diǎn)應(yīng)為祖先節(jié)點(diǎn)
    }
  }
})

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

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

relations 定義段


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

屬性 類型 是否必填 描述
type String 目標(biāo)組件的相對(duì)關(guān)系,可選的值為 parent 、 child 、 ancestor 、 descendant
linked Function 關(guān)系生命周期函數(shù),當(dāng)關(guān)系被建立在頁(yè)面節(jié)點(diǎn)樹(shù)中時(shí)觸發(fā),觸發(fā)時(shí)機(jī)在組件attached生命周期之后
linkChanged Function 關(guān)系生命周期函數(shù),當(dāng)關(guān)系在頁(yè)面節(jié)點(diǎn)樹(shù)中發(fā)生改變時(shí)觸發(fā),觸發(fā)時(shí)機(jī)在組件moved生命周期之后
unlinked Function 關(guān)系生命周期函數(shù),當(dāng)關(guān)系脫離頁(yè)面節(jié)點(diǎn)樹(shù)時(shí)觸發(fā),觸發(fā)時(shí)機(jī)在組件detached生命周期之后
target String 如果這一項(xiàng)被設(shè)置,則它表示關(guān)聯(lián)的目標(biāo)節(jié)點(diǎn)所應(yīng)具有的behavior,所有擁有這一behavior的組件節(jié)點(diǎn)都會(huì)被關(guān)聯(lián)


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)