You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Addition of new RouterLink prop to receive an instance of useLink's returned value instead of internally creating one with the currently required to prop, that is, either to or the new prop should be provided. This enables convenient consumption of useLink's reactive variables (like isActive) in components relying on RouterLink while preventing the creation of an additional useLink's instance.
Basic example
<scriptsetup>constlink=useLink({/* ... */})// useLink's instance available during setup...</script><template><!-- Provide link implementation --><RouterLink:link></RouterLink></template>
Motivation
RouterLink exposes through slot props internal state that is useful to add to or extend its functionality. However, when using composition API, it may be more convenient to access that internal state in the setup function. This might be specially the case for components extending/wrapping RouterLink.
Although there are some ways to extract RouterLink's internal useLink instance, providing it through a prop is probably the most simple, API-friendly, and convenient way.
Detailed design
For the purpose of this discussion, let the new RouterLink prop's name be link.
The implementation would require:
Making RouterLink.to prop not required (when defining component props).
Adding the new link prop with UseLinkReturn type.
In RouterLink's setup, instead of directly calling useLink, check for a provided link prop:
- const link = reactive(useLink(props))+ const link = reactive(props.link ?? useLink(props))
In addition, the RouterLinkProps type could be defined as an union, so that either the to or link prop is provided. The RouterLinkOptions and RouterLinkProps could be updated in the following way:
Components extending RouterLink might need to refactor types to support new link prop.
Alternatives
Working alternatives
Extract RouterLink slot props.
A functional component could be used to extract slot props, but it's comparatively more cumbersome, and data is not really available during setup, but until the component is being mounted.
Create a Link component.
The useLink composable could be use for convenienve in the setup function, but in order not to create a duplicate useLink instance, a new Link component could be implemented. However, for scenarios where nothing additional to RouterLink is required, a new Link component would rewrite a big part, if not all, of RouterLink's implementation.
Use useLink for setup and RouterLink for template independently.
This is the closest API-wise to the proposed API, but as mentioned, two identical useLink instances would be created.
Expose internal RouterLinkuseLink instance (with setup's ctx.expose). This is similar to extracting the RouterLink slot props; the API would be somewhat improved with useTemplateRef, but I think the drawbacks are the same.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
RouterLinkprop to provideuseLinkimplementation #2567Summary
Addition of new
RouterLinkprop to receive an instance ofuseLink's returned value instead of internally creating one with the currently requiredtoprop, that is, eithertoor the new prop should be provided. This enables convenient consumption ofuseLink's reactive variables (likeisActive) in components relying onRouterLinkwhile preventing the creation of an additionaluseLink's instance.Basic example
Motivation
RouterLinkexposes through slot props internal state that is useful to add to or extend its functionality. However, when using composition API, it may be more convenient to access that internal state in the setup function. This might be specially the case for components extending/wrappingRouterLink.Although there are some ways to extract
RouterLink's internaluseLinkinstance, providing it through a prop is probably the most simple, API-friendly, and convenient way.Detailed design
For the purpose of this discussion, let the new
RouterLinkprop's name belink.The implementation would require:
RouterLink.toprop not required (when defining component props).linkprop withUseLinkReturntype.RouterLink's setup, instead of directly callinguseLink, check for a providedlinkprop:In addition, the
RouterLinkPropstype could be defined as an union, so that either thetoorlinkprop is provided. TheRouterLinkOptionsandRouterLinkPropscould be updated in the following way:viewTransitionis moved toRouterLinkOptions.RouterLinkPropsrenamed toRouterLinkBaseProps.RouterLinkPropsunion type.Drawbacks
RouterLinkmight need to refactor types to support newlinkprop.Alternatives
Working alternatives
Extract
RouterLinkslot props.A functional component could be used to extract slot props, but it's comparatively more cumbersome, and data is not really available during setup, but until the component is being mounted.
Create a
Linkcomponent.The
useLinkcomposable could be use for convenienve in the setup function, but in order not to create a duplicateuseLinkinstance, a newLinkcomponent could be implemented. However, for scenarios where nothing additional toRouterLinkis required, a newLinkcomponent would rewrite a big part, if not all, ofRouterLink's implementation.Use
useLinkfor setup andRouterLinkfor template independently.This is the closest API-wise to the proposed API, but as mentioned, two identical
useLinkinstances would be created.This approach is used to implement
NuxtLink. InNuxtLink's implementation,useNuxtLinkis called in the setup function (which internally calls vue-router'suseLink), and thenRouterLinkis rendered.Implementation alternatives
Expose internal
RouterLinkuseLinkinstance (with setup'sctx.expose). This is similar to extracting theRouterLinkslot props; the API would be somewhat improved withuseTemplateRef, but I think the drawbacks are the same.Beta Was this translation helpful? Give feedback.
All reactions