html - 是否可以有条件地渲染布局?

标签 html vue.js vuejs3

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

<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>

但是对于大型组件,这很快就会变得困惑+我必须维护相同的代码两次。可以通过将 //组件内容 移动到它自己的组件中并导入两次来简化它。有没有更优雅的方法来做到这一点?

这是一个布局示例:

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

最佳答案

这是动态组件的情况:

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

对于更复杂的表达式,可以将其移至计算属性。

关于html - 是否可以有条件地渲染布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72901341/

相关文章:

html - 在 CSS 中指定命名颜色的数值

css - html5 图片标题不显示

vue.js - 如何在asyncData nuxtjs中获取上下文和环境变量

vue.js - Vue 3 createApp 未在 Chrome 中定义

vue.js - Vue 3 Composition API - 如何在 setup() 中指定 Prop

css - 我该如何解决这个样式问题?

javascript - map 在 Chrome 中有效,但在 Firefox 中无效

vue.js - 如何使用vuetify在scss文件中创建断点?

javascript - 如何使用 Vue.js 将 TypeScript 文件编译为 JavaScript

javascript - Vue3 Reactive 值未按预期更新