2

I am using v-for directive to get URL name. However, I am facing difficulty in passing the value obtained from a instance of v-for as URL name.

<template>
  <v-list-tile class="pl-5" v-for="choice in choices" :key="choice.name">
    <v-list-title-content>
      <router-link to="'/' + choice.name">
        <v-list-title-title class="white--text headline">
          {{choice.name}}
        </v-list-title-title>
      </router-link>
    </v-list-title-content>
  </v-list-tile>
</template>

<script>
export default{
  data(){
    return{
      choices:[
         name:'A',
         name:'B',
         name:'C',
         name:'D'
      ]    
    }
  }
}
</script>

5 Answers 5

9

You need to bind to. Try something like this

<router-link :to="'/' + choice.name">
Sign up to request clarification or add additional context in comments.

Comments

1

Well, In order to make any attributes to dynamic, you need to use v-bind directive.

Short-hand property for v-bind is :

Hence you can use it as below

<router-link v-bind:to="'/' + choice.name">

With short-hand

<router-link :to="'/' + choice.name">

Official v-bind doc

Hope it helps!

Comments

0

Example with Data():

<router-link :to="myURL">My Link</router-link>

export default {
    // ...
    data() {
        return {
            myURL: '/my-page-link'
        }
    }
    // ...
}

Example without data()

<router-link :to="/my-page-link">My Link</router-link>

Note: v-bind OR : before to attribute

Comments

0

one tip:

when using v-bind like:

// Box.vue
<router-link id="box" :to="{ path: pathto }">{{ name }}</router-link>

don't do the fool thing like me as below:

<Box name="Frontend" pathto="/box/frontend" />
<Box />
<Box />

then you break down.

Comments

0

You need to bind the choice name like any other variable property: Use v-bind:to or the shorthand form :to.

Here are the docs to v-bind: https://v2.vuejs.org/v2/api/#v-bind

You can also directly bind an object to the to directive, passing along the name of the route, so that you need to concat a path. Like this:

<router-link :to="{ name: 'choice1' }">Link</router-link>

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.