{ path: '/repeat/:r+', name: 'repeat', component: components.Bar },
{ path: '/to-p/:p', redirect: to => `/p/${to.params.p}` },
{ path: '/before-leave', component: components.BeforeLeave },
+ {
+ path: '/parent',
+ meta: { fromParent: 'foo' },
+ component: components.Foo,
+ children: [
+ { path: 'child', meta: { fromChild: 'bar' }, component: components.Foo },
+ ],
+ },
{
path: '/inc-query-hash',
redirect: to => ({
expect(stringifyQuery).toHaveBeenCalledWith({ foo: 'bar' })
})
+ it('merges meta properties from parent to child', async () => {
+ const { router } = await newRouter()
+ expect(router.resolve('/parent')).toMatchObject({
+ meta: { fromParent: 'foo' },
+ })
+ expect(router.resolve('/parent/child')).toMatchObject({
+ meta: { fromParent: 'foo', fromChild: 'bar' },
+ })
+ })
+
it('can do initial navigation to /', async () => {
const router = createRouter({
history: createMemoryHistory(),
path,
params,
matched,
- meta: matcher ? matcher.record.meta : {},
+ meta: mergeMetaFields(matched),
}
}
return false
}
+/**
+ * Merge meta fields of an array of records
+ *
+ * @param matched array of matched records
+ */
+function mergeMetaFields(matched: MatcherLocation['matched']) {
+ return matched.reduce(
+ (meta, record) => ({
+ ...meta,
+ ...record.meta,
+ }),
+ {} as MatcherLocation['meta']
+ )
+}
+
export { PathParserOptions }