Skip to content

Add RouterLink prop to provide useLink implementation #2567

@8ctavio

Description

@8ctavio

What problem is this solving

I am writing a component that wraps RouterLink, but I would like to use the useLink composable to easily access refs like isActive in the setup function.

In order to use both useLink and RouterLink in a single component, a new RouterLink prop could be introduced so that useLink's returned value may be provided instead of the to prop. The implementation can be easily achieved; the main concern would be updating RouterLinkProps type. An union could be used to define RouterLinkProps so that either to or the new prop are provided.

Apart from simplified link's consumption/access, I think another secondary benefit would be that if RouterLink gets unmounted (e.g., with v-if), useLink does not have to be called on every mount.

Proposed solution

<script setup>
const link = useLink()
// ... use link during setup
</script>

<template>
  <!-- Provide link implementation -->
  <RouterLink :link>

  </RouterLink>
</template>

Describe alternatives you've considered

  1. Use useLink only: Requires rewriting part of RouterLink.
  2. Use RouterLink only: Requires extracting its slot props from the template for use in the setup function, which is somewhat cumbersome, and they become available until the mounting phase.
  3. Use both independently: Would create two different sets of computed values and functions that do the same, one for the setup function, and one for the RouterLink component.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions