【问题标题】:VueJS 2 conditional rendering in value binding值绑定中的 VueJS 2 条件渲染
【发布时间】:2017-08-30 20:17:32
【问题描述】:

我只是尝试让 VueJS 2 在向 dom 元素添加值时呈现内联条件。我知道,可以使用v-if 让元素根据条件出现或消失,但是如何使用 inline-condition

我举个例子。以下 html 描述了我的想法 我知道这行会产生错误<span> 两个元素都由允许它们出现或不出现的条件控制,这很好用。

现在,我尝试根据条件(例如,在括号中)将值绑定到href 属性取决于

<div id="vuemain">
<span v-if="diced < 6">Looser</span>
<span v-if="diced == 6">Winner</span>
<a :href="'https://link-to-whatever.com/'+{diced==6 : 'winner', diced<6 : 'looser'} ">LINK</a>
</div>

所以在 VueJS 渲染之后,&lt;a&gt; 标签应该是这样的:

<a href="https://link-to-whatever.com/winner"> <!-- If diced == 6 -->

<a href="https://link-to-whatever.com/looser"> <!-- If diced < 6 -->

你明白我的问题是什么吗?这可能吗?

在此先感谢

艾伦

【问题讨论】:

    标签: javascript vue.js vuejs2


    【解决方案1】:

    这应该可行。

    <a :href="'https://link-to-whatever.com/'+ (diced==6 ? 'winner' : 'looser')">LINK</a>
    

    看起来您正在尝试使用对象语法,但在这种情况下它不会真正起作用。相反,只需使用上面的三元。

    【讨论】:

      猜你喜欢
      • 2020-08-22
      • 2021-02-12
      • 2018-07-04
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 2018-06-11
      • 2018-12-25
      • 2020-11-15
      相关资源
      最近更新 更多