Kaiser91
Kaiser91

Reputation: 343

Vue js dynamically render router link path

I'm struggling to find a way to render the path of the router-link dynamically, for example with the test variable. I'm trying to bind :to but unsuccessfully. I don't know if I can use the ternary operator in the binding as shown below:

<template>
  <div>
    <router-link
       :to="{ test ? '/success' : '/fail' }"
       tag="button"
       class="btn-next">
       <span class="btn-text">BUTTON</span>
    </router-link>
  </div>
</template>

<script> 
export default {
  // ...
  data: function() {
    return {
      test: false
    };
  }
}
</script>

Upvotes: 0

Views: 788

Answers (1)

tony19
tony19

Reputation: 138696

Your solution is close (you don't need the curly brackets). It should look like this:

<router-link :to="test ? '/success' : '/fail'">

demo

Upvotes: 3

Related Questions