无遮无挡三级动态图,熟女人妻高清一区二区三区,午夜精品一区二区三区 ,色吧成人网,无码爆乳护士让我爽

私域電商軟件及相關(guān)增值服務(wù)提供商    

免費(fèi)試用
微信小程序開(kāi)發(fā)組件間關(guān)系
作者: --時(shí)間: 2025-08-10 17:14:06 閱讀量:

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

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

對(duì)于復(fù)雜的自定義組件,組件之間的通訊往往比較困難。在組件定義時(shí)添加 relations 關(guān)系定義段可以解決這個(gè)問(wèn)題。

示例中的 custom-ul 和 custom-li 都是自定義組件,添加如下關(guān)系定義后,它們之間的通訊就變得方便了。

// 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: function(target) {
        // 每次有custom-li被插入時(shí)執(zhí)行,target是該節(jié)點(diǎn)實(shí)例對(duì)象,觸發(fā)在該節(jié)點(diǎn)attached生命周期之后
      },
      linkChanged: function(target) {
        // 每次有custom-li被移動(dòng)后執(zhí)行,target是該節(jié)點(diǎn)實(shí)例對(duì)象,觸發(fā)在該節(jié)點(diǎn)moved生命周期之后
      },
      unlinked: function(target) {
        // 每次有custom-li被移除時(shí)執(zhí)行,target是該節(jié)點(diǎn)實(shí)例對(duì)象,觸發(fā)在該節(jié)點(diǎn)detached生命周期之后
      }
    }
  },
  methods: {
    _getAllLi: function(){
      // 使用getRelationNodes可以獲得nodes數(shù)組,包含所有已關(guān)聯(lián)的custom-li,且是有序的
      var nodes = this.getRelationNodes('path/to/custom-li')
    }
  },
  ready: function(){
    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: function(target) {
        // 每次被插入到custom-ul時(shí)執(zhí)行,target是custom-ul節(jié)點(diǎn)實(shí)例對(duì)象,觸發(fā)在attached生命周期之后
      },
      linkChanged: function(target) {
        // 每次被移動(dòng)后執(zhí)行,target是custom-ul節(jié)點(diǎn)實(shí)例對(duì)象,觸發(fā)在moved生命周期之后
      },
      unlinked: function(target) {
        // 每次被移除時(shí)執(zhí)行,target是custom-ul節(jié)點(diǎn)實(shí)例對(duì)象,觸發(fā)在detached生命周期之后
      }
    }
  }
})

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

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

有時(shí)需要關(guān)聯(lián)的是一類組件,這時(shí)可以使用 behavior 進(jìn)行設(shè)置。對(duì)于 custom-form 組件想要關(guān)聯(lián) custom-input 和 custom-submit 兩個(gè)組件,只需讓這兩個(gè)組件都引用同一個(gè) behavior:

// path/to/custom-form-controls.js
module.exports = Behavior({
  // ...
})
// path/to/custom-input.js
var 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
var 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)
    }
  }
})

在關(guān)系定義中,可以使用相同的 behavior 來(lái)代替具體的組件路徑:

// path/to/custom-form.js
var 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
    }
  }
})

3. relations 定義段

relations

一套系統(tǒng)全搞定
  • 商家管理
  • 商品管理
  • 訂單管理
  • 會(huì)員管理
  • 營(yíng)銷中心
  • 供應(yīng)鏈入駐
  • 財(cái)務(wù)管理
  • 支付分賬
  • 商城直播
免費(fèi)試用
更多產(chǎn)品任你選
B2B2C多用戶商城系統(tǒng)

類天貓&京東模式系統(tǒng)

了解更多
B2B2B電商交易系統(tǒng)

全渠道訂貨/采購(gòu)及經(jīng)銷商管
理數(shù)字化系統(tǒng)

了解更多
S2B2B電商交易系統(tǒng)

上下游資源整合數(shù)字化解決方

了解更多
企業(yè)集采商城系統(tǒng)

中大型企業(yè)數(shù)字化采購(gòu)與交易
系統(tǒng)

了解更多
員工福利商城系統(tǒng)

集福利管理、發(fā)放于一體的員
工福利商城

了解更多

電話咨詢 微信咨詢 0元開(kāi)店