组件间关系 relations的一些疑惑
发布于 5 年前 作者 gang26 10019 次浏览 来自 问答

两个自定义组件产生关联必须要像下面的结构吗

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

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

</custom-ul>

像简单的单选框/多选框采用这样的结构没什么问题,但有时候会出现子组件作为另外一个组件的slot。这个时候结构就变了

<custom-ul>

 <other-com>

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

</other-com>

<other-com>

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

</other-com>

</custom-ul>

这样的结构是relations无法生效的。是不是这种设计组件的思想不对。请指正。

还是应该将结构改成这样

<custom-ul>

 <custom-li>

  <other-com></other-com>

 </custom-li>

 <custom-li>

  <other-com></other-com>

 </custom-li>

</custom-ul>

1 回复

relations 的 type 可选值有四个: parent / child / ancestor / descendant 。前两个表示父子节点关系;后两个表示祖先后代节点关系。你的情况选后两个就可以了。

回到顶部