日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)小程序教程:微信小程序組件間關(guān)系

組件間關(guān)系

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

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

我們注重客戶提出的每個(gè)要求,我們充分考慮每一個(gè)細(xì)節(jié),我們積極的做好成都網(wǎng)站建設(shè)、成都做網(wǎng)站服務(wù),我們努力開拓更好的視野,通過不懈的努力,創(chuàng)新互聯(lián)公司贏得了業(yè)內(nèi)的良好聲譽(yù),這一切,也不斷的激勵(lì)著我們更好的服務(wù)客戶。 主要業(yè)務(wù):網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),微信小程序開發(fā),網(wǎng)站開發(fā),技術(shù)開發(fā)實(shí)力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫(kù)的技術(shù)開發(fā)工程師。


   item 1 
   item 2 

這個(gè)例子中, custom-ul 和 custom-li 都是自定義組件,它們有相互間的關(guān)系,相互間的通信往往比較復(fù)雜。此時(shí)在組件定義時(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: 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ì)生效。

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

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


  
    input
    
  
   submit 

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
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)
    }
  }
})

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

// 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
    }
  }
})

relations 定義段

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

選項(xiàng) 類型 是否必填 描述
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í)觸發(fā),觸發(fā)時(shí)機(jī)在組件attached生命周期之后
linkChanged Function 關(guān)系生命周期函數(shù),當(dāng)關(guān)系在頁(yè)面節(jié)點(diǎn)樹中發(fā)生改變時(shí)觸發(fā),觸發(fā)時(shí)機(jī)在組件moved生命周期之后
unlinked Function 關(guān)系生命周期函數(shù),當(dāng)關(guān)系脫離頁(yè)面節(jié)點(diǎn)樹時(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)


分享標(biāo)題:創(chuàng)新互聯(lián)小程序教程:微信小程序組件間關(guān)系
URL標(biāo)題:http://www.dlmjj.cn/article/cdodsoh.html