有时,自定义组件模版中的一些节点,其对应的自定义组件不是由自定义组件本身确定的,而是自定义组件的调用者确定的。这时可以把这个节点声明为“抽象节点”。例如,我们现在来实现一个“选框组”(selectable-group)组件,它其中可以放置单选框(custom-radio)或者复选框(custom-checkbox)。这个组件的ttml/wxml可以这样编写:在selectable-group.ttml中:view:for={{labels}}labelselectabledisabled={{false}}/selectable{{item}}/label/view其中,selectable不是任何在json文件的usingComponents字段中声明的组件,而是一个抽象节点。它需要在componentGenerics字段中声明:{componentGenerics:{selectable:true}}
使用包含抽象节点的组件在使用selectable-group组件时,必须指定selectable具体是哪个组件:selectable-groupgeneric:selectable=custom-radio/这样,在生成这个selectable-group组件的实例时,selectable节点会生成custom-radio组件实例。类似地,如果这样使用:selectable-groupgeneric:selectable=custom-checkbox/selectable节点则会生成custom-checkbox组件实例。注意:上述的custom-radio和custom-checkbox需要包含在这个ttml对应json文件的usingComponents定义段中。{usingComponents:{custom-radio:path/to/custom/radio,custom-checkbox:path/to/custom/checkbox}}
抽象节点的默认组件抽象节点可以指定一个默认组件,当具体组件未被指定时,将创建默认组件的实例。默认组件可以在componentGenerics字段中指定:{componentGenerics:{selectable:{default:path/to/default/component}}}注意:节点的generic引用generic:xxx=yyy中,值yyy只能是静态值,不能包含数据绑定。因而抽象节点特性并不适用于动态决定节点名的场景。了解更多