舟山闪爸电子技术有限公司

最新微信小程序組件間關(guān)系,小程序關(guān)聯(lián)一類組件

  組件間關(guān)系

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

  有時需要實現(xiàn)這樣的組件:

最新微信小程序組件間關(guān)系,小程序關(guān)聯(lián)一類組件

  

   item 1

   item 2

  

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

  // path/to/custom-ul.js

  Component({

  relations: {

  './custom-li': {

  type: 'child', // 關(guān)聯(lián)的目標節(jié)點應(yīng)為子節(jié)點

  linked: function(target) {

  // 每次有custom-li被插入時執(zhí)行,target是該節(jié)點實例對象,觸發(fā)在該節(jié)點attached生命周期之后

  },

  linkChanged: function(target) {

  // 每次有custom-li被移動后執(zhí)行,target是該節(jié)點實例對象,觸發(fā)在該節(jié)點moved生命周期之后

  },

  unlinked: function(target) {

  // 每次有custom-li被移除時執(zhí)行,target是該節(jié)點實例對象,觸發(fā)在該節(jié)點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)的目標節(jié)點應(yīng)為父節(jié)點

  linked: function(target) {

  // 每次被插入到custom-ul時執(zhí)行,target是custom-ul節(jié)點實例對象,觸發(fā)在attached生命周期之后

  },

  linkChanged: function(target) {

  // 每次被移動后執(zhí)行,target是custom-ul節(jié)點實例對象,觸發(fā)在moved生命周期之后

  },

  unlinked: function(target) {

  // 每次被移除時執(zhí)行,target是custom-ul節(jié)點實例對象,觸發(fā)在detached生命周期之后

  }

  }

  }

  })

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

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

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

  

  

  input

  

  

   submit

  

  custom-form 組件想要關(guān)聯(lián) custom-input 和 custom-submit 兩個組件。此時,如果這兩個組件都有同一個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)的目標節(jié)點應(yīng)為祖先節(jié)點

  }

  }

  })

  // path/to/custom-submit.js

  var customFormControls = require('./custom-form-controls')

  Component({

  behaviors: [customFormControls],

  relations: {

  './custom-form': {

  type: 'ancestor', // 關(guān)聯(lián)的目標節(jié)點應(yīng)為祖先節(jié)點

  }

  }

  })

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

  // path/to/custom-form.js

  var customFormControls = require('./custom-form-controls')

  Component({

  relations: {

  'customFormControls': {

  type: 'descendant', // 關(guān)聯(lián)的目標節(jié)點應(yīng)為子孫節(jié)點

  target: customFormControls

  }

  }

  })


【本站聲明】
  1、本站文章中所選用的圖片及文字來源于網(wǎng)絡(luò)以及用戶投稿,由于未聯(lián)系到知識產(chǎn)權(quán)人或未發(fā)現(xiàn)有關(guān)知識產(chǎn)權(quán)的登記,如有知識產(chǎn)權(quán)人并不愿意我們使用,如果有侵權(quán)請立即聯(lián)系。
  2、本網(wǎng)站不對文章中所涉及的內(nèi)容真實性、準確性、可靠性負責,僅系客觀性描述,如您需要了解該類商品/服務(wù)詳細的資訊,請您直接與該類商品/服務(wù)的提供者聯(lián)系。


KESION 科汛軟件

KESION 科汛軟件是國內(nèi)領(lǐng)先的在線教育軟件及私域社交電商軟件服務(wù)提供商,長期專注于為企業(yè)提供在線教育軟件及社交電商SaaS平臺解決方案。
公司核心產(chǎn)品云開店SaaS社交電商服務(wù)平臺、在線教育SaaS服務(wù)平臺、教育企業(yè)數(shù)字化SaaS云平臺、企微營銷助手、私有化獨立部署品牌網(wǎng)校和在線教育咨詢等。

KESION 不斷通過技術(shù)創(chuàng)新,提供產(chǎn)品和服務(wù),助力企業(yè)向數(shù)字化轉(zhuǎn)型,通過科技驅(qū)動商業(yè)革新,讓商業(yè)變得更智慧!



▼點擊進入科汛官網(wǎng)了解更多



熱門標簽
微信小程序 SaaS
上/下篇
  • 小程序綁定公眾號怎么關(guān)聯(lián),小程序綁定公眾號的具體過程

  • 微信小程序demo源碼怎么樣

換一換相關(guān)推薦
精選內(nèi)容
熱點精選