]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat: type components for a route record
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 2 May 2019 13:43:01 +0000 (15:43 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 2 May 2019 13:43:01 +0000 (15:43 +0200)
src/router.ts
src/types/index.ts

index 7419763c3da9a1995f05b9c18ce67d1d405e6dd0..0c4922ef544c1bfc6bca67fa47b76844a00edb8a 100644 (file)
@@ -103,16 +103,20 @@ export class Router {
     // TODO: ensure we are leaving since we could just be changing params or not changing anything
     // TODO: is it okay to resolve all matched component or should we do it in order
     await Promise.all(
-      from.matched.map(async ({ component }) => {
+      from.matched.map(async record => {
         // TODO: cache async routes per record
-        const resolvedComponent = await (typeof component === 'function'
-          ? component()
-          : component)
-        if (resolvedComponent.beforeRouteLeave) {
-          // TODO: handle the next callback
-          guards.push(
-            guardToPromiseFn(resolvedComponent.beforeRouteLeave, to, from)
-          )
+        // TODO: handle components version. Probably repfactor in extractComponentGuards
+        if ('component' in record) {
+          const { component } = record
+          const resolvedComponent = await (typeof component === 'function'
+            ? component()
+            : component)
+          if (resolvedComponent.beforeRouteLeave) {
+            // TODO: handle the next callback
+            guards.push(
+              guardToPromiseFn(resolvedComponent.beforeRouteLeave, to, from)
+            )
+          }
         }
       })
     )
@@ -157,18 +161,21 @@ export class Router {
     await Promise.all(
       to.matched.map(async record => {
         // TODO: cache async routes per record
-        const { component } = record
-        const resolvedComponent = await (typeof component === 'function'
-          ? component()
-          : component)
-        if (
-          resolvedComponent.beforeRouteEnter &&
-          from.matched.indexOf(record)
-        ) {
-          // TODO: handle the next callback
-          guards.push(
-            guardToPromiseFn(resolvedComponent.beforeRouteEnter, to, from)
-          )
+        // TODO: handle components version. Probably repfactor in extractComponentGuards
+        if ('component' in record) {
+          const { component } = record
+          const resolvedComponent = await (typeof component === 'function'
+            ? component()
+            : component)
+          if (
+            resolvedComponent.beforeRouteEnter &&
+            from.matched.indexOf(record)
+          ) {
+            // TODO: handle the next callback
+            guards.push(
+              guardToPromiseFn(resolvedComponent.beforeRouteEnter, to, from)
+            )
+          }
         }
       })
     )
index 59f84558015dc0f3954a712c399702458e470e72..c170af8fcd26b55a7b6f03db53379eaa264b1f9a 100644 (file)
@@ -80,20 +80,30 @@ export interface RouteComponentInterface {
 // export type RouteComponent = TODO & RouteComponentInterface
 export type RouteComponent = {
   template?: string
+  render?: Function
 } & RouteComponentInterface
 
 // NOTE not sure the whole PropsTransformer thing can be usefull
 // since in callbacks we don't know where we are coming from
 // and I don't thin it's possible to filter out the route
 // by any means
-export interface RouteRecord {
+
+interface RouteRecordCommon {
   path: string // | RegExp
-  component: RouteComponent | Lazy<RouteComponent>
   name?: string
   beforeEnter?: NavigationGuard
-  // props: PT
 }
 
+interface RouteRecordSingleView extends RouteRecordCommon {
+  component: RouteComponent | Lazy<RouteComponent>
+}
+
+interface RouteRecordMultipleViews extends RouteRecordCommon {
+  components: Record<string, RouteComponent | Lazy<RouteComponent>>
+}
+
+export type RouteRecord = RouteRecordSingleView | RouteRecordMultipleViews
+
 export const START_RECORD: RouteRecord = {
   path: '/',
   // @ts-ignore