]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
use current route
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 13 Feb 2019 12:18:26 +0000 (13:18 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 13 Feb 2019 12:18:26 +0000 (13:18 +0100)
explorations/html5.ts
src/index.ts
src/types/index.ts

index 9d5983d292d91ba1607ebe02ab9f19f8337b5b11..32db56ec3d3abd81fbab56c0a12dbca9bb338a31 100644 (file)
@@ -39,3 +39,16 @@ r.push({
     id: '6',
   },
 })
+
+r.push({
+  name: 'user',
+  params: {
+    id: '5',
+  },
+})
+
+r.push({
+  params: {
+    id: 'no-name',
+  },
+})
index 68c0efe0f7e0003362a8af9ddbcd9e62211594b0..988af460a2c344a372cfd27c66c3d13ad956a1b4 100644 (file)
@@ -1,6 +1,6 @@
 import BaseHistory from './history/base'
 import pathToRegexp from 'path-to-regexp'
-import { Location, RouteRecord, ParamsType } from './types/index'
+import { Location, RouteRecord, ParamsType, START_RECORD } from './types/index'
 
 interface RouterOptions {
   history: BaseHistory
@@ -14,25 +14,31 @@ interface RouteMatcher {
   keys: string[]
 }
 
+function generateMatcher(record: RouteRecord) {
+  const keys: pathToRegexp.Key[] = []
+  // TODO: if children use option end: false ?
+  const re = pathToRegexp(record.path, keys)
+  return {
+    re,
+    resolve: pathToRegexp.compile(record.path),
+    keys: keys.map(k => '' + k.name),
+    record,
+  }
+}
+
+const START_MATCHER = generateMatcher(START_RECORD)
+
 export class Router {
   protected history: BaseHistory
   private routes: RouteMatcher[]
+  currentRoute: RouteRecord = START_RECORD
+  currentMatcher: RouteMatcher = START_MATCHER
 
   constructor(options: RouterOptions) {
     this.history = options.history
     this.history.ensureLocation()
 
-    this.routes = options.routes.map(record => {
-      const keys: pathToRegexp.Key[] = []
-      // TODO: if children use option end: false ?
-      const re = pathToRegexp(record.path, keys)
-      return {
-        re,
-        resolve: pathToRegexp.compile(record.path),
-        keys: keys.map(k => '' + k.name),
-        record,
-      }
-    })
+    this.routes = options.routes.map(generateMatcher)
   }
 
   /**
@@ -53,23 +59,28 @@ export class Router {
    * passed, it returns the string itself
    * @param location Location to resolve to a url
    */
-  resolve(location: Location): string {
+  resolve(location: Readonly<Location>): string {
     if (typeof location === 'string') return location
     if ('path' in location) {
       // TODO: convert query, hash, warn params
       return location.path
     }
 
+    let matcher: RouteMatcher | void
     if (!('name' in location)) {
       // TODO: use current location
       // location = {...location, name: this.}
-      return '/using current location'
+      matcher = this.routes.find(r => r.record.name === this.currentRoute.name)
+      // return '/using current location'
+    } else {
+      matcher = this.routes.find(r => r.record.name === location.name)
     }
-    const matcher = this.routes.find(r => r.record.name === location.name)
+
     if (!matcher) {
       // TODO: error
       throw new Error('No match for' + location)
     }
+
     return matcher.resolve(location.params || {})
   }
 }
index 928202832fbce7ad6865cde02453c1bf1aabd231..9e1a4339648b41bf1801111e28a9e48154af14fe 100644 (file)
@@ -31,12 +31,20 @@ export type Location =
     }
   | {
       name: string
-      params?: Record<string, string>
+      params?: ParamsType
+    }
+  | {
+      params: ParamsType
     }
 
 export type HistoryLocation = string
 
 export const START: HistoryLocation = '/'
+export const START_RECORD: RouteRecord = {
+  path: '/',
+  // @ts-ignore
+  component: { render: h => h() },
+}
 
 export interface NavigationCallback {
   (to: HistoryLocation, from: HistoryLocation): void