gpt4 book ai didi

html - 具有相同 v-if 的两个 div 元素或一个包装器?

转载 作者:行者123 更新时间:2023-12-05 01:25:59 24 4
gpt4 key购买 nike

在这种情况下,您认为最佳做法是什么:

<div>
<div v-if="condition-1">Show something</div>
<div v-if="condition-2">Show something else</div>
<div v-if="condition-2">Show some other thing</div>
</div>

<div>
<div v-if="condition-1">Show something</div>
<div v-if="condition-2">
<div>Show something else</div>
<div>Show some other thing</div>
</div>
</div>

我想这归结为您如何评价 html 元素,对我来说,第一个选项是我更喜欢编写它的方式,因为我发现额外的 div 在第二个选项中没有服务于结构目的。您如何支持或反对这两者?

[编辑]如下面的答案所示,使用模板元素来保存条件是可行的方法

最佳答案

在这种情况下,我可能会使用 template 作为这两个项目的包装器。如果这 3 个项目应该处于同一级别,则更有意义。

https://v2.vuejs.org/v2/guide/conditional.html#Conditional-Groups-with-v-if-on-lt-template-gt

<div>
<div v-if="condition-1">Show something</div>
<template v-if="condition-2"> <!-- Or v-else -->
<div>Show something else</div>
<div>Show some other thing</div>
</template>
</div>

关于html - 具有相同 v-if 的两个 div 元素或一个包装器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70566289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com