0

I simply try to let VueJS 2 render a inline condition while I add a value to an dom element. I know, that it is possible to use v-if to let elements appear or disappear based on conditions, but how can I use a inline-condition?

I will give an example. The following html describe my idea and I know that this lines generates an error. Both <span> elements are controlled by conditions which lets them appear or not and this works fine.

Now, I try to bind a value to the href attribute depending on a condition (which are in the parentheses for example).

<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>

So after rendering by VueJS the <a> tag should be like:

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

OR

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

Do you understand what my problem is and is that somehow possible?

Many thanks in advance

Allan

1 Answer 1

3

This should work.

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

It looks like you were trying to use the object syntax, which won't really work in this case. Instead, just use the ternary above.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.