组件间关系
界说和使用组件间关系
有时需要实现这样的组件:
这个例子中,, 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
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
}
}
})
KESION pp电子软件
KESION pp电子软件是海内领先的在线教育软件及私域社交电商软件服务提供商,,恒久专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。。
公司焦点产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化自力安排品牌网校和在线教育咨询等。。KESION 一直通过手艺立异,,提供产品和服务,,助力企业向数字化转型,,通过科技驱动商业刷新,,让商业变得更智慧!
微信之以是受到这么多人的喜欢和支持,,不但仅由于它齐全的语音谈天功效和视频谈天功效。。这些年来微信的公众号也受到许多人的支持。。并且现在微信公众号也受到许多人的支持和热...
提及微信小程序demo源码,,肯定许多人还不太相识。。微信小程序就是一种不需要下载,,可以直接在微信客户端使用的软件,,一最先微信在不太成熟的情形下,,并没有小程序。。可是近年来,,...