使用template和slot创建弹性模版,提高组件的灵活性
template
在网页上使用重复的html的时候,使用模版<template>
比直接复制显然是更好的选择,不会被DOM直接的渲染出来,但通过javascript可以获取到
<template id="custome-detail">
<style>
fieldset {
border: none;
border-top: 1px solid #cccccc;
}
</style>
<fieldset>
<legend>
custome fieldset
</legend>
<p>this is a paragraph</p>
</fieldset>
</template>
slot
槽(slot)是一个html占位符,使用name
属性作为id
,将上面的模版修改如下
<template id="custome-fieldset">
<style>
fieldset {
border: none;
border-top: 1px solid #cccccc;
}
</style>
<fieldset>
<legend align="center">
<slot name="title">custome fieldset</slot>
</legend>
<slot name="content">this is a paragraph</slot>
</fieldset>
</template>
在html中的使用:
<custome-fieldset>
<span slot="title">Hello world</span>
<p slot="content">This is content</p>
</custome-fieldset>
<custome-fieldset>
<span slot="title">list</span>
<dl slot="content">
<dt>name</dt>
<dd>Abelce</dd>
</dl>
<dl slot="content">
<dt>sex</dt>
<dd>male</dd>
</dl>
</custome-fieldset>
代码[github](