最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js: how to use the logical Operators in a template? - Stack Overflow

programmeradmin0浏览0评论

I would simply like to use 2 different v-if into the same div, as the following:

Actually I have this code:

<div class="o-overlay" v-if="show">
    <div class="o-overlay__bg" @click="hide"></div>
</div>

Containing only one v-if.

And I would like to use 2 or more v-if inside the same condition, like for instance:

<div class="o-overlay" v-if="show" v-if="visible">
    <div class="o-overlay__bg" @click="hide"></div>
</div>

But it give me a lot of error and I would simply like to learn the correct way to use it. Thank you in advance :)

I would simply like to use 2 different v-if into the same div, as the following:

Actually I have this code:

<div class="o-overlay" v-if="show">
    <div class="o-overlay__bg" @click="hide"></div>
</div>

Containing only one v-if.

And I would like to use 2 or more v-if inside the same condition, like for instance:

<div class="o-overlay" v-if="show" v-if="visible">
    <div class="o-overlay__bg" @click="hide"></div>
</div>

But it give me a lot of error and I would simply like to learn the correct way to use it. Thank you in advance :)

Share Improve this question edited Jul 23, 2020 at 22:37 Boussadjra Brahim 1 asked Jan 7, 2019 at 15:05 CedCed 1,5396 gold badges24 silver badges36 bronze badges 3
  • 1 You can do show || visible which the item will show if either are true or show && visible where you will only show if both are true. – user5283119 Commented Jan 7, 2019 at 15:10
  • Possible duplicate of add-more-than-one-v-if-with-different-conditions-for-one-div-without-duplicate – Moumen Soliman Commented Jan 7, 2019 at 15:43
  • Possible duplicate of VueJS Multiple Condition v-if – user5283119 Commented Jan 7, 2019 at 15:44
Add a comment  | 

1 Answer 1

Reset to default 16

You could use them in the same v-if directive e.g.

&& = Logical Operator AND

|| = Logical Operator OR

&& means both conditions have to be true for the div to show.

<div class="o-overlay" v-if="show && visible">
    <div class="o-overlay__bg" @click="hide"></div>
</div>

|| means only one of the conditions have to be true for the div to show.

<div class="o-overlay" v-if="show || visible">
    <div class="o-overlay__bg" @click="hide"></div>
</div>
发布评论

评论列表(0)

  1. 暂无评论