在Vue.js的`v-if`条件中如何链式多个条件?

huangapple go评论79阅读模式
英文:

How do I chain multiple conditions in a Vue JS v-if conditonal?

问题

<div class="person" v-for="user in users" v-if="user.country === '美国' || user.country === '日本'">
    <span>{{ user.first_name }}, {{ user.last_name }}</span>
    <a :href="'mailto:' + user.email.toLowerCase()">{{ user.email }}</a>
    <span>{{ user.country }}</span>
</div>
英文:

I am trying to only show results that match several countries. I must have the syntax wrong and I have tried various different ways. Any help would be greatly appreciated.

&lt;div class=&quot;person&quot; v-for=&#39;user in users&#39; v-if=&quot;user.country === &#39;United States || user.country === Japan&#39;&quot;&gt;
        &lt;span&gt;{{ user.first_name }}, {{ user.last_name }}&lt;/span&gt;
        &lt;a :href=&quot;&#39;mailto:&#39; + user.email | lowercase&quot;&gt;{{ user.email }}&lt;/a&gt;
        &lt;span&gt;{{ user.country }}&lt;/span&gt;
      &lt;/div&gt;
    ```

</details>


# 答案1
**得分**: 1

你有一个错误 - 未闭合的引号,修复方法是:

```html
v-if="user.country === 'United States' || user.country === 'Japan'"

但是Vue3文档说:

不建议在同一元素上同时使用v-if和v-for,因为存在隐式优先级。请参考样式指南获取详细信息。

所以你可能应该这样做:

<template v-for="user in users">
  <div class="person" v-if="user.country === 'United States' || user.country === 'Japan'">
    <span>{{ user.first_name }}, {{ user.last_name }}</span>
    <a :href="'mailto:' + user.email | lowercase">{{ user.email }}</a>
    <span>{{ user.country }}</span>
  </div>
</template>

但是我的首选方式是使用计算属性:

<script setup>
// ...
const filteredUsers = computed(() => users.filter(user => ['United States', 'Japan'].includes(user.country)));
// ...
</script>
<template>
  <div class="person" v-for="user in filteredUsers">
    <span>{{ user.first_name }}, {{ user.last_name }}</span>
    <a :href="'mailto:' + user.email | lowercase">{{ user.email }}</a>
    <span>{{ user.country }}</span>
  </div>
</template>
英文:

You have an error - an unclosed quote, the fix is:

v-if=&quot;user.country === &#39;United States&#39; || user.country === &#39;Japan&#39;&quot;

But the Vue3 docs say:

> It's not recommended to use v-if and v-for on the same element due to implicit precedence. Refer to style guide for details.

so you should probably do:

&lt;template v-for=&quot;user in users&quot;&gt;
&lt;div class=&quot;person&quot; v-if=&quot;user.country === &#39;United States&#39; || user.country === &#39;Japan&#39;&quot;&gt;
        &lt;span&gt;{{ user.first_name }}, {{ user.last_name }}&lt;/span&gt;
        &lt;a :href=&quot;&#39;mailto:&#39; + user.email | lowercase&quot;&gt;{{ user.email }}&lt;/a&gt;
        &lt;span&gt;{{ user.country }}&lt;/span&gt;
      &lt;/div&gt;
&lt;/template&gt;

But my preferred way is to use a computed:

&lt;script setup&gt;
...
const filteredUsers = computed(() =&gt; users.filter(user =&gt; [&#39;United States&#39;, &#39;Japan&#39;].includes(user.country)));
...
&lt;/script&gt;
&lt;template&gt;
&lt;div class=&quot;person&quot; v-for=&quot;user in filteredUsers&quot;&gt;
        &lt;span&gt;{{ user.first_name }}, {{ user.last_name }}&lt;/span&gt;
        &lt;a :href=&quot;&#39;mailto:&#39; + user.email | lowercase&quot;&gt;{{ user.email }}&lt;/a&gt;
        &lt;span&gt;{{ user.country }}&lt;/span&gt;
      &lt;/div&gt;
&lt;/template

huangapple
  • 本文由 发表于 2023年6月8日 07:01:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定