在vuejs中将旧代码修改为vue router 4

Modifying old code to vue router 4 in vuejs

在 Vue Router 4 之前,我有以下标记:

<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>

对于 Vue Router 4,我现在有以下警告:

[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. 

如何将此代码更改为 Vue Router 4 推荐?

基于 docs for <router-link>'s v-slot,Vue Router 4 中的等价物为:

<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>

!ICanGo(item) 时不需要禁用该元素,因为在这种情况下 <span> 本身没有 link 激活。

demo

如果你有一个table使用它的责任,而不是使用这种方法

  <div class="w-full overflow-x-auto">
      <table class="w-full">
        <tbody>
          <nuxt-link
            v-for="(item, index) in tableItems"
            :key="index"
            to="/somewhere"
            tag="tr"
            class="
              duration-300
              rounded
              whitespace-nowrap
              border-b-2 border-gray-200 border-transparent
              cursor-pointer
              hover:bg-gray-50
            "
            @click.native="doSomething"
          >
            <td class="py-2 w-1/3">
              <img src="~/assets/images/img.jpg" />
            </td>
            <td class="py-2 w-1/3">
              <p>Something 1</p>
            </td>
            <td class="py-2 w-1/3">
              <p>Something 2</p>
            </td>
          </nuxt-link>
        </tbody>
      </table>
    </div>

这会警告你,如果你使用 @tony19 方法可能会破坏线路并破坏 table 的责任,而不是 @tony19 方法和以上做喜欢这个:

<div class="w-full overflow-x-auto">
  <table class="w-full">
    <nuxt-link
      v-for="(item, index) in tableItems"
      :key="index"
      to="/somewhere-good"
      @click.native="doSomething"
    >
      <tbody>
        <tr
          class="
            duration-300
            rounded
            whitespace-nowrap
            border-b-2 border-gray-200 border-transparent
            cursor-pointer
            hover:bg-gray-50
          "
        >
          <td class="py-2 w-1/3">
            <img src="~/assets/images/img.jpg" />
          </td>
          <td class="py-2 w-1/3">
            <p>Something 1</p>
          </td>
          <td class="py-2 w-1/3">
            <p>Something 2</p>
          </td>
        </tr>
      </tbody>
    </nuxt-link>
  </table>
</div>

我确实使用了 nuxt.js 标签 link 也就是 nuxt-link 但如果你也使用 router-link 就没问题了,你可能知道我的元素 类 是 tailwindcss

注意:如果您的点击事件不起作用,请删除 .native

编辑: 最好使用 table 的自然结构而不使用 anuxt-link 进行导航,而是使用 router.push