Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying old code to vue router 4 in vuejs

Before Vue Router 4, I had the following markup:

<router-link
              :disabled="!ICanGo(item)"
              :tag="!ICanGo(item) ? 'span' : 'a'"
              :event="ICanGo(item) ? 'click' : ''"
              :to="{ name: 'editUsuario', params: { id: item.id } }"
>{{ item.nome }}</router-link>

With Vue Router 4, I have the following warning now:

[vue-router]
<router-link>'s tag prop is deprecated and has been removed in Vue Router 4. Use the v-slot API to remove this warning: https://next.router.vuejs.org/guide/migration/#removal-of-event-and-tag-props-in-router-link. 

How could I change this code to Vue Router 4 recommendations?

like image 548
Luiz Alves Avatar asked Jan 27 '21 18:01

Luiz Alves


People also ask

Does Vue router work with Vue 2?

Only Vue Router 2 is compatible with Vue 2, so if you're updating Vue, you'll have to update Vue Router as well.


Video Answer


1 Answers

Based on the docs for <router-link>'s v-slot, the equivalent in Vue Router 4 would be:

<router-link
  :to="{ name: 'editUsuario', params: { id: item.id } }"
  custom
  v-slot="{ href, navigate }"
>
  <span v-if="!ICanGo(item)">{{ item.nome }}</span>
  <a v-else :href="href" @click="navigate">{{ item.nome }}</a>
</router-link>

There's no need for disabling the element when !ICanGo(item) because the <span> itself has no link activation in that case.

demo

like image 135
tony19 Avatar answered Jan 03 '23 23:01

tony19