Lars-Janssen's avatar

Vue.js href

Hi,

I'm using vue.js in combination with blade.

(So I do not use vue-router)

What is the best way to link in a foreach loop with a slug?

This is not working:

<tr v-for="user in users">
    <td><a href="/profile/{{ user.slug }}">{{ user. name }}</td>
</tr>
0 likes
3 replies
JackRobertson's avatar

You need to tell blade not to parse the data in your curly braces. You can do this by prefixing them with an @. For example:

<tr v-for="user in users">
    <td><a href="/profile/@{{ user.slug }}">@{{ user.name }}</a></td>
</tr>
Lars-Janssen's avatar

@JackRobertson I'm using .vue files so that should not matter. But when I try this:

<tr v-for="user in users" v-bind:href="profileUrl(user)">
    <td>{{ user.name }}</td>
    <td>{{ user.corporation.name }}</td>
    <td class="hidden-xs hidden-sm">
        <span class="label label-success">{{ user.messages_count }}</span>
    </td>
    <td>
        <span class="label label-success" v-if="isAdmin(user)">Admin</span>
        <span class="label label-danger" v-if="isNotActive(user)">Inactief</span>
    </td>
</tr>
</tbody>

I can see this:

But it does not work -_-

samfrjn11's avatar
Level 3

If you want to use data from your vue data you have to bind href like this:

<tr v-for="user in users">
    <td><a :href="'/profile/' + user.slug">{{ user.name }}</a></td>
</tr>

Note that in your first example there was a space between user. and name 'user._name'. Also you didn't close the a-tag.

Don't use href attribute on a tr, it's not designed for this. If you really want to make a whole tr clickable, add an event listener to it.

<tr v-for="user in users" @click="userProfile(user)">
    <td>{{ user.name }}</td>
</tr>

and then create a method for your vue instance / export

methods: {
    userProfile(user){
        window.location.href = '/profile/' + user.slug;
    }
}
8 likes

Please or to participate in this conversation.