routes: [
{ path: '/', component },
{ path: '/users/:id', name: 'user', component },
- { path: /^\/about\/?$/, component },
+ // { path: /^\/about\/?$/, component },
],
})
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: '/',
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
interface RouteMatcher {
re: RegExp
+ resolve: (params: ParamsType) => string
record: RouteRecord
keys: string[]
}
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,
+ }
})
}
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 || {})
}
}
// 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
name: string
params?: Record<string, string>
}
+
export type HistoryLocation = string
export const START: HistoryLocation = '/'
+
+export interface NavigationCallback {
+ (to: HistoryLocation, from: HistoryLocation): void
+}
+
+export type RemoveListener = () => void