Skip to content

Commit

Permalink
feat(types): RouterConfig for multiple components (#3217) (#3218)
Browse files Browse the repository at this point in the history
* feat(types): RouterConfig for multiple components (#3217)

* Apply suggestions from code review

Co-authored-by: Eduardo San Martin Morote <[email protected]>
  • Loading branch information
javiertury and posva authored Jun 4, 2020
1 parent ba0bffa commit dab86c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 14 additions & 5 deletions types/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,30 @@ export interface PathToRegexpOptions {
end?: boolean
}

export interface RouteConfig {
interface _RouteConfigBase {
path: string
name?: string
component?: Component
components?: Dictionary<Component>
redirect?: RedirectOption
alias?: string | string[]
children?: RouteConfig[]
meta?: any
beforeEnter?: NavigationGuard
props?: boolean | Object | RoutePropsFunction
caseSensitive?: boolean
pathToRegexpOptions?: PathToRegexpOptions
}

interface RouteConfigSingleView extends _RouteConfigBase {
component?: Component
props?: boolean | Object | RoutePropsFunction
}

interface RouteConfigMultipleViews extends _RouteConfigBase {
components?: Dictionary<Component>
children?: RouteConfig[]
props?: Dictionary<boolean | Object | RoutePropsFunction>
}

export type RouteConfig = RouteConfigSingleView | RouteConfigMultipleViews

export interface RouteRecord {
path: string
regex: RegExp
Expand Down
3 changes: 3 additions & 0 deletions types/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Vue.use(VueRouter)
const Home = { template: '<div>home</div>' }
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
const Abc = { template: '<div>abc</div>' }
const Async = () => Promise.resolve({ template: '<div>async</div>' })

const Hook: ComponentOptions<Vue> = {
Expand Down Expand Up @@ -76,6 +77,7 @@ const router = new VueRouter({
components: {
default: Foo,
bar: Bar,
abc: Abc,
asyncComponent: Async
},
meta: { auth: true },
Expand All @@ -88,6 +90,7 @@ const router = new VueRouter({
props: {
default: true,
bar: { id: 123 },
abc: route => route.params,
asyncComponent: (route: Route) => route.params
}
},
Expand Down

4 comments on commit dab86c5

@GUEThe
Copy link

@GUEThe GUEThe commented on dab86c5 Jun 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cause type error when traverse RouteConfig[] , read property children.

Property 'children' does not exist on type 'RouteConfig'.
Property 'children' does not exist on type 'RouteConfigSingleView'.

@bijiasuo2006
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I met the same error
Property 'component' does not exist on type 'RouteConfig'.
Property 'component' does not exist on type 'RouteConfigMultipleViews'.

@posva
Copy link
Member

@posva posva commented on dab86c5 Jun 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The children property shouldn't have been moved out. I will fix it and release a new patch

@lambdalisue
Copy link

@lambdalisue lambdalisue commented on dab86c5 Aug 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@posva It seems children has fixed by c1df447#diff-82f268a0cfa99495f4791a5e93d5e183 but I still have an error on component as @bijiasuo2006 said.

zsh 2020-08-17 12-04-10

I needed to annotate it like

    const _r = (r as any) as {
      component?: typeof Vue;
    };
    const component = _r.component ? _r.component : BareLayout;

Please sign in to comment.