前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【愚公系列】2022年02月 微信小程序-Component组件的关系

【愚公系列】2022年02月 微信小程序-Component组件的关系

作者头像
愚公搬代码
发布2022-03-01 11:40:08
2710
发布2022-03-01 11:40:08
举报
文章被收录于专栏:历史专栏历史专栏

文章目录

前言

relations 定义段包含目标组件路径及其对应选项,可包含的选项见下表。

选项

类型

是否必填

描述

type

String

目标组件的相对关系,可选的值为 parent 、 child 、 ancestor 、 descendant

linked

Function

关系生命周期函数,当关系被建立在页面节点树中时触发,触发时机在组件attached生命周期之后

linkChanged

Function

关系生命周期函数,当关系在页面节点树中发生改变时触发,触发时机在组件moved生命周期之后

unlinked

Function

关系生命周期函数,当关系脱离页面节点树时触发,触发时机在组件detached生命周期之后

target

String

如果这一项被设置,则它表示关联的目标节点所应具有的behavior,所有拥有这一behavior的组件节点都会被关联

一、组件间关系

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

1.index页面

代码语言:javascript
复制
{
  "usingComponents": {
    "custom-ul": "/components/custom-ul-component",
    "custom-li": "/components/custom-li-component"
  }
}
代码语言:javascript
复制
<custom-ul>
  <custom-li> item 1 </custom-li>
  <custom-li> item 2 </custom-li>
</custom-ul>

2.custom-ul

代码语言:javascript
复制
// path/to/custom-ul.js
Component({
  relations: {
    './custom-li-component': {
      type: 'child', // 关联的目标节点应为子节点
      linked: function (target) {
        // 每次有custom-li被插入时执行,target是该节点实例对象,触发在该节点attached生命周期之后
        console.log('[custom-ul] a child is linked: ', target)
      },
      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('./custom-li-component')
      console.log(nodes)
    }
  },
  ready: function () {
    this._getAllLi()
  }
})
代码语言:javascript
复制
<!-- 组件模板 -->
<view class="wrapper">
  <slot></slot>
</view>

3.custom-ul

代码语言:javascript
复制
// path/to/custom-li.js
Component({
  relations: {
    './custom-ul-component': {
      type: 'parent', // 关联的目标节点应为父节点
      linked: function (target) {
        // 每次被插入到custom-ul时执行,target是custom-ul节点实例对象,触发在attached生命周期之后
        console.log('child linked to ', target)
      },
      linkChanged: function (target) {
        // 每次被移动后执行,target是custom-ul节点实例对象,触发在moved生命周期之后
      },
      unlinked: function (target) {
        // 每次被移除时执行,target是custom-ul节点实例对象,触发在detached生命周期之后
      }
    }
  }
})
代码语言:javascript
复制
<text> li </text>

二、关联一类组件

1.index页面

代码语言:javascript
复制
{
  "usingComponents": {
    "custom-input": "../components/custom-input",
    "custom-submit": "../components/custom-submit",
    "custom-form": "../components/custom-form"
  }
}
代码语言:javascript
复制
<custom-form>
  <view>
    input
    <custom-input></custom-input>
  </view>
  <custom-submit> submit </custom-submit>
</custom-form>
代码语言:javascript
复制
// path/to/custom-form-controls.js
module.exports = Behavior({
  // ...
})
在这里插入图片描述
在这里插入图片描述

2.custom-form

代码语言:javascript
复制
var customFormControls = require('./custom-form-controls')
Component({
  relations: {
    'customFormControls': {
      type: 'descendant', // 关联的目标节点应为子孙节点
      target: customFormControls,
      linked: function(target) {
        console.info('已关联到 ' + target.is)
      }
    }
  }
})

3.custom-input

代码语言:javascript
复制
// path/to/custom-input.js
var customFormControls = require('./custom-form-controls')
Component({
  behaviors: [customFormControls],
  relations: {
    './custom-form': {
      type: 'ancestor', // 关联的目标节点应为祖先节点
    }
  }
})

4.custom-form

代码语言:javascript
复制
// path/to/custom-submit.js
var customFormControls = require('./custom-form-controls')
Component({
  behaviors: [customFormControls],
  relations: {
    './custom-form': {
      type: 'ancestor', // 关联的目标节点应为祖先节点
    }
  }
})
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-02-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 前言
  • 一、组件间关系
    • 1.index页面
      • 2.custom-ul
        • 3.custom-ul
        • 二、关联一类组件
          • 1.index页面
            • 2.custom-form
              • 3.custom-input
                • 4.custom-form
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档