mpx 中手写一个关联组件 relations 使用

前文
父组件 tabbar
子组件 tabbar-item


  • 关键点 relations 中如何在.mpx找到准确的关联组件的路径
  • 父组件代码
//mpx ?resolve找到component组件hash路径 
import tabbarItem from '../tabbar-item/index?resolve'  

createComponent({
	relations : {
      [ tabbarItem ] : {
        type : 'descendant',
        linked : function ( target ) {
          var _this = this;
          
          this.items.push(target)
          
          setTimeout(() => {
            _this.setActiveItem()
          })
        },
        unlinked : function ( target ) {
          var _this2 = this;
          this.items = this.items.filter(function ( item ) {
            return item !== target;
          });
          setTimeout(function () {
            _this2.setActiveItem();
          });
        }
      }
    }
})
  • 子组件代码
import tabbar from '../tabbar/index?resolve' 
createComponent({
    relations: {
      [tabbar]:{
        type: 'ancestor'
			}
    }
})

获取子组件或父组件 详细文档移步wx小程序官方文档
https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/relations.html

  • 以子组件获取父组件为例
let parent = this.getRelationNodes([tabbar])[0]
组件使用和效果图

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

ps: 重点在与mpx中 组件关联要使用 【…?resolve】 前面路径具体到.mpx文件 但省略.mpx后缀即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值