vue.js - 分离条件渲染的开始和结束标签

标签 vue.js vuejs2 vue-component

<template>
    <div>
        <template v-if="isFieldsetElement">
            <fieldset>
                <legend>Label Here</legend>
        </template>
        <template v-else>
            <label>Label Here</label>
        </template>

        <span> Description </span>
        <img src="image.png" />
        <div>more stuff here </div>

        <template v-if="isFieldsetElement">
            </fieldset>
        </template>
    </div>
</template>

我无法进行此标记分离。基本上我有一个单独的 headerfooter(因为缺少更好的词)但是我不能像在服务器端生成的标记中那样在 Vue 中做到这一点。

这个问题是否有解决方法或更优雅的解决方案?

最佳答案

使用 Dynamic Components :

<component :is="isFieldsetElement ? 'fieldset' : 'div'">
    <component :is="isFieldsetElement ? 'legend' : 'label'">
        <span> Description </span>
        <img src="image.png" />
        <div>more stuff here </div>
    </component>
</component>

关于vue.js - 分离条件渲染的开始和结束标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53421978/

相关文章:

javascript - 未知的自定义元素: <router-link> in VueJS

javascript - Vuex getter 在数组更新后不更新值

vuejs2 - Vue.js - 元素 UI - 表单项标签槽

vue.js - 具有外部模板和样式的 Vue 组件?

javascript - Vue.js 修改计算属性?

javascript - Ant 设计 Vue : Carousel arrows disappear when there's only one div content

javascript - 如何将样式类应用于按钮,但在 Vue 中任何时候只能激活一个按钮?

javascript - 响应式图库 Vue.js

javascript - 为什么此函数仅在 IE11 上需要分号?

javascript - 如果数据元素的索引发生变化,则重新渲染 DOM