最新微信小程序组件间关系,小程序关联一类组件

  组件间关系

  定义和使用组件间关系

  有时需要实现这样的组件:

最新微信小程序组件间关系,小程序关联一类组件

  

   item 1

   item 2

  

  这个例子中, custom-ul 和 custom-li 都是自定义组件,它们有相互间的关系,相互间的通信往往比较复杂。此时在组件定义时加入 relations 定义段,可以解决这样的问题。示例:

  // path/to/custom-ul.js

  Component({

  relations: {

  './custom-li': {

  type: 'child', // 关联的目标节点应为子节点

  linked: function(target) {

  // 每次有custom-li被插入时执行,target是该节点实例对象,触发在该节点attached生命周期之后

  },

  linkChanged: function(target) {

  // 每次有custom-li被移动后执行,target是该节点实例对象,触发在该节点moved生命周期之后

  },

  unlinked: function(target) {

  // 每次有custom-li被移除时执行,target是该节点实例对象,触发在该节点detached生命周期之后

  }

  }

  },

  methods: {

  _getAllLi: function(){

  // 使用getRelationNodes可以获得nodes数组,包含所有已关联的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', // 关联的目标节点应为父节点

  linked: function(target) {

  // 每次被插入到custom-ul时执行,target是custom-ul节点实例对象,触发在attached生命周期之后

  },

  linkChanged: function(target) {

  // 每次被移动后执行,target是custom-ul节点实例对象,触发在moved生命周期之后

  },

  unlinked: function(target) {

  // 每次被移除时执行,target是custom-ul节点实例对象,触发在detached生命周期之后

  }

  }

  }

  })

  注意:必须在两个组件定义中都加入relations定义,否则不会生效。

  关联一类组件

  有时,需要关联的是一类组件,如:

  

  

  input

  

  

   submit

  

  custom-form 组件想要关联 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', // 关联的目标节点应为祖先节点

  }

  }

  })

  // path/to/custom-submit.js

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

  Component({

  behaviors: [customFormControls],

  relations: {

  './custom-form': {

  type: 'ancestor', // 关联的目标节点应为祖先节点

  }

  }

  })

  则在 relations 关系定义中,可使用这个behavior来代替组件路径作为关联的目标节点:

  // path/to/custom-form.js

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

  Component({

  relations: {

  'customFormControls': {

  type: 'descendant', // 关联的目标节点应为子孙节点

  target: customFormControls

  }

  }

  })


【本站声明】
  1、本站文章中所选用的图片及文字来源于网络以及用户投稿,由于未联系到知识产权人或未发现有关知识产权的登记,如有知识产权人并不愿意我们使用,如果有侵权请立即联系。
  2、本网站不对文章中所涉及的内容真实性、准确性、可靠性负责,仅系客观性描述,如您需要了解该类商品/服务详细的资讯,请您直接与该类商品/服务的提供者联系。


KESION 科汛软件

KESION 科汛软件是国内领先的在线教育软件及私域社交电商软件服务提供商,长期专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。
公司核心产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化独立部署品牌网校和在线教育咨询等。

KESION 不断通过技术创新,提供产品和服务,助力企业向数字化转型,通过科技驱动商业革新,让商业变得更智慧!



▼点击进入科汛官网了解更多



热门标签
微信小程序 SaaS
上/下篇
  • 小程序绑定公众号怎么关联,小程序绑定公众号的具体过程

  • 微信小程序demo源码怎么样

换一换相关推荐
精选内容
热点精选