关于微信小程序组件间关系的问题?

不太明白,微信小程序中组件间关系relations的作用。文档中实例是这样的:

<custom-ul>
  <custom-li> item 1 </custom-li>
  <custom-li> item 2 </custom-li>
</custom-ul>
// path/to/custom-ul.js
Component({
  relations: {
    './custom-li': {
      type: 'child' // 关联的目标节点应为子节点
    }
  },
  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' // 关联的目标节点应为父节点
    }
  }
})

以下是我的问题:

实例中组件的结构已经表明了嵌套关系

<custom-ul>
  <custom-li> item 1 </custom-li>
  <custom-li> item 2 </custom-li>
</custom-ul>

那么请问relations这个自定义字段,代表什么意思呢?有什么具体用途实例?

阅读 1.7k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题