我想根据以下条件更改组件中的布局:

<template>
  <layoutA>
    //component contents
  </layoutA>
</template>

为此:

<template>
  <layoutB>
    //component contents
  </layoutB>
</template>

我知道如何使用条件渲染组件的唯一方法是使用v-if,如果不满足条件,它不会渲染子组件.我可以这样做:

<template>
  <layoutA v-if="condition">
    //component contents
  </layoutA>
  <layoutB v-else>
    //component contents
  </layoutB>
</template>

but with large components this gets messy quickly + I would have to maintain the same code twice. It could be simplified by moving //component contents into its own component and import it twice. Is there a more elegant way to do this?

以下是layout的示例:

<template>
  <v-nav/>
  <slot/>
  <v-footer/>
</template>

推荐答案

这是动态组件的情况:

  <componenent :is="condition ? 'layoutA' : 'layoutB'">
    //component contents
  </componenent>

对于更复杂的表达式,可以将其移动到计算(computed)属性.

Html相关问答推荐

如何打破长URL,包含连字符在HTML与CSS

创建具有圆锥渐变的水平CSS聚光灯

如何防止第二列中的内容影响第一列中内容的位置?

react :事件和转发器在特定代码段中不起作用

Flexbox在元素之间造成了太大的差距

错误时交换表单,成功时使用HTMX重定向

如何在没有包装元素的情况下在React(Next.js)中呈现HTML注释

如何将多个类名组合到CSS中的一个关键帧

如何为高度较小的块制作边框动画?

在 flexbox 中使用 spacer 是否有效

在选项标签中如何添加Django模板的条件

样式化单选按钮 背景 colored颜色

如何使用CSS Select 同级元素的::before伪元素?

如何配置 prettier 使其以 Vue.js 模板语法的特定方式运行?

使用 R 中的 rvest 包获取搜索结果的第一个链接

如何从 razor 页面打开 html 页面

暗模式转换器

如何自定义具有白色透明背景的光标图像?

AWS SES:如何正确使用 CSS

如何在 about:blank 网站上设置页面标题