2

I am trying to do the following in Vue.js

     <table>
        <tr v-for="item in messages">
            <td><router-link to="'/user/' + item.id">{{ item.name }}</router-link></td>
        </tr>
    </table>

My messages property has an array of objects in it. Each object has an id property, which I want to be appended on to my to attribute. So if I had two items in the messages array with ids of 1 and 2, I would expect to get something like this:

     <table>
        <tr v-for="item in messages">
            <td><router-link to="/user/1">{{ item.name }}</router-link></td>
        </tr>
        <tr v-for="item in messages">
            <td><router-link to="/user/2">{{ item.name }}</router-link></td>
        </tr>
    </table>

I also tried putting doing {{ message.id }}, but that gives me an interpolation error. How can I get this to render the actual value for message.id in the "to" attribute? The item.name attribute renders as expected.

1 Answer 1

3

Just add v-bind: before to

<table>
    <tr v-for="item in messages">
        <td><router-link v-bind:to="'/user/' + item.id">{{ item.name }}</router-link></td>
    </tr>
</table>

Without v-bind: you're literally using 'user' + item.id as value. For more clarification check this docs section

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.