From: Eduardo San Martin Morote Date: Tue, 12 Feb 2019 09:45:48 +0000 (+0100) Subject: keep history state X-Git-Tag: v4.0.0-alpha.0~475 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bb1d043a35804447f866996c5561dfa05088acdf;p=thirdparty%2Fvuejs%2Frouter.git keep history state --- diff --git a/explorations/html5.ts b/explorations/html5.ts index 6ed60906..9d5983d2 100644 --- a/explorations/html5.ts +++ b/explorations/html5.ts @@ -8,7 +8,7 @@ const r = new Router({ routes: [ { path: '/', component }, { path: '/users/:id', name: 'user', component }, - { path: /^\/about\/?$/, component }, + // { path: /^\/about\/?$/, component }, ], }) @@ -19,9 +19,15 @@ window.h = h window.r = r h.listen((to, from) => { - console.log({ to, from }) + console.log('popstate', { to, from }) }) +// h.push('/hey') +// h.push('/hey?lol') +// h.push('/foo') +// h.push('/replace-me') +// h.replace('/bar') + r.push('/about') r.push({ path: '/', diff --git a/src/index.ts b/src/index.ts index 14b7c8b2..68c0efe0 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 } from './types/index' +import { Location, RouteRecord, ParamsType } from './types/index' interface RouterOptions { history: BaseHistory @@ -9,6 +9,7 @@ interface RouterOptions { interface RouteMatcher { re: RegExp + resolve: (params: ParamsType) => string record: RouteRecord keys: string[] } @@ -25,7 +26,12 @@ export class Router { const keys: pathToRegexp.Key[] = [] // TODO: if children use option end: false ? const re = pathToRegexp(record.path, keys) - return { re, keys: keys.map(k => '' + k.name), record } + return { + re, + resolve: pathToRegexp.compile(record.path), + keys: keys.map(k => '' + k.name), + record, + } }) } @@ -57,15 +63,13 @@ export class Router { if (!('name' in location)) { // TODO: use current location // location = {...location, name: this.} - return '/heeey' + return '/using current location' } const matcher = this.routes.find(r => r.record.name === location.name) if (!matcher) { // TODO: error throw new Error('No match for' + location) } - matcher.re - return '/TODO' - // this.matcher.match(location) + return matcher.resolve(location.params || {}) } } diff --git a/src/types/index.ts b/src/types/index.ts index 39a94194..92820283 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -17,7 +17,7 @@ export type ParamsType = Record // and I don't thin it's possible to filter out the route // by any means export interface RouteRecord { - path: string | RegExp + path: string // | RegExp component: TODO name?: string // props: PT @@ -33,6 +33,13 @@ export type Location = name: string params?: Record } + export type HistoryLocation = string export const START: HistoryLocation = '/' + +export interface NavigationCallback { + (to: HistoryLocation, from: HistoryLocation): void +} + +export type RemoveListener = () => void