From fd4dc0630bdf856f972ed6e9020b70a70ac582b4 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 16 Mar 2020 09:37:11 +0100 Subject: [PATCH] feat(view): allow props as object in record --- __tests__/RouterView.spec.ts | 25 +++++++++++++++++++++++-- __tests__/utils.ts | 13 +++++++++++++ src/components/View.ts | 6 ++++-- src/types/index.ts | 2 +- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/__tests__/RouterView.spec.ts b/__tests__/RouterView.spec.ts index 17dd39f7..fdb28b65 100644 --- a/__tests__/RouterView.spec.ts +++ b/__tests__/RouterView.spec.ts @@ -74,9 +74,9 @@ const routes = createRoutes({ matched: [{ components: { foo: components.Foo }, path: '/' }], }, withParams: { - fullPath: '/users/3', + fullPath: '/users/1', name: undefined, - path: '/users/3', + path: '/users/1', query: {}, params: { id: '1' }, hash: '', @@ -89,6 +89,22 @@ const routes = createRoutes({ }, ], }, + withIdAndOther: { + fullPath: '/props/1', + name: undefined, + path: '/props/1', + query: {}, + params: { id: '1' }, + hash: '', + meta: {}, + matched: [ + { + components: { default: components.WithProps }, + path: '/users/:id', + props: { id: 'foo', other: 'fixed' }, + }, + ], + }, }) describe('RouterView', () => { @@ -173,4 +189,9 @@ describe('RouterView', () => { await tick() expect(el.innerHTML).toBe(`
User: 4
`) }) + + it('can pass an object as props', async () => { + const { el } = factory(routes.withIdAndOther) + expect(el.innerHTML).toBe(`
id:foo;other:fixed
`) + }) }) diff --git a/__tests__/utils.ts b/__tests__/utils.ts index 0a0b718c..6e2c3ac7 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -95,6 +95,19 @@ export const components = { return h('div', {}, 'User: ' + this.id) }, }), + WithProps: defineComponent({ + props: { + id: { + default: 'default', + }, + other: { + default: 'other', + }, + }, + render() { + return h('div', {}, `id:${this.id};other:${this.other}`) + }, + }), Nested: { render: () => { const RouterView = resolveComponent('RouterView') diff --git a/src/components/View.ts b/src/components/View.ts index 2016759c..341e2dc2 100644 --- a/src/components/View.ts +++ b/src/components/View.ts @@ -37,9 +37,11 @@ export const View = defineComponent({ ) const propsData = computed(() => { - if (!matchedRoute.value.props) return {} + const { props } = matchedRoute.value + if (!props) return {} + if (props === true) return route.value.params - return route.value.params + return props }) provide(matchedRouteKey, matchedRoute) diff --git a/src/types/index.ts b/src/types/index.ts index d9238df6..fd3cd6b1 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -118,7 +118,7 @@ export interface RouteRecordCommon { path: string alias?: string | string[] name?: string - props?: boolean + props?: boolean | Record // TODO: beforeEnter has no effect with redirect, move and test beforeEnter?: NavigationGuard | NavigationGuard[] meta?: Record -- 2.39.5