From: Eduardo San Martin Morote Date: Wed, 13 Feb 2019 12:18:26 +0000 (+0100) Subject: use current route X-Git-Tag: v4.0.0-alpha.0~474 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e359c4c80cad85bcc48a4f0168841e5b39e19a9c;p=thirdparty%2Fvuejs%2Frouter.git use current route --- diff --git a/explorations/html5.ts b/explorations/html5.ts index 9d5983d2..32db56ec 100644 --- a/explorations/html5.ts +++ b/explorations/html5.ts @@ -39,3 +39,16 @@ r.push({ id: '6', }, }) + +r.push({ + name: 'user', + params: { + id: '5', + }, +}) + +r.push({ + params: { + id: 'no-name', + }, +}) diff --git a/src/index.ts b/src/index.ts index 68c0efe0..988af460 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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): 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 || {}) } } diff --git a/src/types/index.ts b/src/types/index.ts index 92820283..9e1a4339 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -31,12 +31,20 @@ export type Location = } | { name: string - params?: Record + 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