assertErrorMatch({ path: '/', components }, { name: 'Home' })
).toMatchSnapshot()
})
+
+ it('merges params', () => {
+ assertRecordMatch(
+ { path: '/:a/:b', name: 'p', components },
+ { name: 'p', params: { b: 'b' } },
+ { name: 'p', path: '/a/b', params: { a: 'a', b: 'b' } },
+ {
+ params: { a: 'a' },
+ path: '/a',
+ matched: [],
+ meta: {},
+ name: undefined,
+ }
+ )
+ })
+
+ it('only keep existing params', () => {
+ assertRecordMatch(
+ { path: '/:a/:b', name: 'p', components },
+ { name: 'p', params: { b: 'b' } },
+ { name: 'p', path: '/a/b', params: { a: 'a', b: 'b' } },
+ {
+ params: { a: 'a', c: 'c' },
+ path: '/a',
+ matched: [],
+ meta: {},
+ name: undefined,
+ }
+ )
+ })
})
describe('LocationAsRelative', () => {
)
})
+ it('merges params', () => {
+ assertRecordMatch(
+ { path: '/:a/:b?', name: 'p', components },
+ { params: { b: 'b' } },
+ { name: 'p', path: '/a/b', params: { a: 'a', b: 'b' } },
+ {
+ name: 'p',
+ params: { a: 'a' },
+ path: '/a',
+ matched: [],
+ meta: {},
+ }
+ )
+ })
+
it('throws if the current named route does not exists', () => {
const record = { path: '/', components }
const start = {
})
name = matcher.record.name
- // TODO: merge params with current location. Should this be done by name. I think there should be some kind of relationship between the records like children of a parent should keep parent props but not the rest
- // needs an RFC if breaking change
- params = location.params || currentLocation.params
+ params = {
+ ...paramsFromLocation(
+ currentLocation.params,
+ matcher.keys.map(k => k.name)
+ ),
+ ...location.params,
+ }
// throws if cannot be stringified
path = matcher.stringify(params)
} else if ('path' in location) {
currentLocation,
})
name = matcher.record.name
- params = location.params || currentLocation.params
+ // since we are navigating to the same location, we don't need to pick the
+ // params like when `name` is provided
+ params = { ...currentLocation.params, ...location.params }
path = matcher.stringify(params)
}
return { addRoute, resolve, removeRoute, getRoutes, getRecordMatcher }
}
+function paramsFromLocation(
+ params: MatcherLocation['params'],
+ keys: string[]
+): MatcherLocation['params'] {
+ let newParams = {} as MatcherLocation['params']
+
+ for (let key of keys) {
+ if (key in params) newParams[key] = params[key]
+ }
+
+ return newParams
+}
+
/**
* Normalizes a RouteRecordRaw. Transforms the `redirect` option into a `beforeEnter`
* @param record