Vue: 怎么没有类似v-elseif的指令

Created on 16 Feb 2016  ·  12Comments  ·  Source: vuejs/vue

很多时候都需要有多个分支的选择,但目前只有v-if和v-else,这是为什么呢?有什么好的方式转换?

Most helpful comment

这样?

<div v-if="xxx"></div>
<template v-else>
  <div v-if="yyy"></div>
  <div v-else></div>
</template>

All 12 comments

这样?

<div v-if="xxx"></div>
<template v-else>
  <div v-if="yyy"></div>
  <div v-else></div>
</template>

@hanai 感谢回答。
那么有没有这样的呢?

<div v-if="xxx"></div>
<div v-elseif="yyy"></div>
...
<div v-else></div>

虽然目前我也是差不多按照您的这种方式,但是感觉稍微有点繁琐,就是想问一下而已。

http://vuejs.org/api/#partial
尝试使用谐音
(Google translate!)

@Mat-Moo 十分感谢。
我的目的只是想根据某个字段的不同状态得到不同的显示而已,
用到的地方不是很多,
按照您的做法会生成太多的partial,这并不是我想要的。

好吧,我使用在某些情况下这种做法,而是取决于你需要达到什么

直接这样就好了:

<div v-if="type === 'xxx'"></div>
<div v-if="type === 'xxx'"></div>
<div v-if="type === 'xxx'"></div>

如果你的情况比这更复杂,可以尝试使用 computed 属性解决问题。

^

@Mat-Moo is one of the best mentor i have ever found...

@yyx990803 大大,你上面的回复是什么意思?

我尝试改写vue
增加vElif,给它增减bind,update,remove方法,可以使用v-elif指令,但v-else失效,主要是考虑到会和v-show结合在一起,所以有点麻烦

elif 这个需求还是很大的, @yyx990803 可以考虑后续添加上吗?

這個問題今天也遇到了,在做表單驗證的時候希望多個錯誤同時只出現一種,

然後讓使用者一個一個解決,但是問題在於 Vue.js 沒有提供 v-elseif

導致兩個欄位錯誤的時候會同時噴兩個訊息出來。

2016-11-17 2 04 09

假設有了 v-elseif,就可以解決上述問題。下面這是期望的做法,兩個錯誤只出現一個錯誤。

2016-11-17 2 02 25


相同請求:

how to use elseif in vue js?
elseif in vuejs ? '-'
希望支持elseif语句
请问如何实现 elseif 的效果?
Feature request: v-elseif

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fergaldoyle picture fergaldoyle  ·  3Comments

loki0609 picture loki0609  ·  3Comments

robertleeplummerjr picture robertleeplummerjr  ·  3Comments

bdedardel picture bdedardel  ·  3Comments

lmnsg picture lmnsg  ·  3Comments