Skip to content

Commit 4142871

Browse files
committed
fix(link): let vue merge attrs
Fix vuejs#846
1 parent add6ce9 commit 4142871

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

__tests__/RouterLink.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,32 @@ describe('RouterLink', () => {
800800
expect(router.push).toHaveBeenCalledTimes(1)
801801
})
802802

803+
it('allows adding more click listeners', async () => {
804+
const onClick = jest.fn()
805+
const { router, wrapper } = await factory(
806+
START_LOCATION_NORMALIZED,
807+
{ to: locations.basic.string, onClick },
808+
locations.basic.normalized
809+
)
810+
wrapper.find('a')!.trigger('click')
811+
expect(router.push).toHaveBeenCalledTimes(1)
812+
expect(onClick).toHaveBeenCalledTimes(1)
813+
})
814+
815+
it('allows adding custom classes', async () => {
816+
const { wrapper } = await factory(
817+
locations.basic.normalized,
818+
{ to: locations.basic.string, class: 'custom class' },
819+
locations.basic.normalized
820+
)
821+
expect(wrapper.find('a')!.classes()).toEqual([
822+
'router-link-active',
823+
'router-link-exact-active',
824+
'custom',
825+
'class',
826+
])
827+
})
828+
803829
it('calls router.replace when clicked with replace prop', async () => {
804830
const { router, wrapper } = await factory(
805831
START_LOCATION_NORMALIZED,

src/RouterLink.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { RouteLocationRaw, VueUseOptions, RouteLocation } from './types'
1616
import { isSameRouteLocationParams, isSameRouteRecord } from './location'
1717
import { routerKey, routeLocationKey } from './injectionSymbols'
1818
import { RouteRecord } from './matcher/types'
19-
import { assign } from './utils'
2019
import { NavigationFailure } from './errors'
2120

2221
export interface RouterLinkOptions {
@@ -145,7 +144,7 @@ export const RouterLinkImpl = /*#__PURE__*/ defineComponent({
145144
},
146145
},
147146

148-
setup(props, { slots, attrs }) {
147+
setup(props, { slots }) {
149148
const link = reactive(useLink(props))
150149
const { options } = inject(routerKey)!
151150

@@ -192,19 +191,16 @@ export const RouterLinkImpl = /*#__PURE__*/ defineComponent({
192191
? children
193192
: h(
194193
'a',
195-
assign(
196-
{
197-
'aria-current': link.isExactActive
198-
? props.ariaCurrentValue
199-
: null,
200-
onClick: link.navigate,
201-
href: link.href,
202-
},
203-
attrs,
204-
{
205-
class: elClass.value,
206-
}
207-
),
194+
{
195+
'aria-current': link.isExactActive
196+
? props.ariaCurrentValue
197+
: null,
198+
href: link.href,
199+
// this would override user added attrs but Vue will still add
200+
// the listener so we end up triggering both
201+
onClick: link.navigate,
202+
class: elClass.value,
203+
},
208204
children
209205
)
210206
}

0 commit comments

Comments
 (0)