<input type="checkbox" onchange="cancel = !cancel" /> Cancel Next
Navigation
</label>
+ <router-view></router-view>
</div>
</body>
</html>
template: `<div>A component</div>`,
}
+const Home: RouteComponent = {
+ template: `<div>Home</div>`,
+}
+
+const User: RouteComponent = {
+ template: `<div>User: {{ $route.params.id }}</div>`,
+}
+
const GuardedWithLeave: RouteComponent = {
template: `<div>
<p>try to leave</p>
const router = new Router({
history: new HTML5History(),
routes: [
- { path: '/', component },
- { path: '/users/:id', name: 'user', component },
+ { path: '/', component: Home },
+ { path: '/users/:id', name: 'user', component: User },
{ path: '/n/:n', name: 'increment', component },
{ path: '/multiple/:a/:b', name: 'user', component },
{
path: '/',
})
- // await r.push({
- // name: 'user',
- // params: {
- // id: '6',
- // },
- // })
+ await r.push({
+ name: 'user',
+ params: {
+ id: '6',
+ },
+ })
- // await r.push({
- // name: 'user',
- // params: {
- // id: '5',
- // },
- // })
+ await r.push({
+ name: 'user',
+ params: {
+ id: '5',
+ },
+ })
// try {
// await r.push({
// @ts-ignore
const route = parent.$route
+ console.log('rendering', route)
+
// TODO: support nested router-views
const matched = route.matched[0]
this._router = this.$options.router
// this._router.init(this)
// @ts-ignore
- Vue.util.defineReactive(this, '_route', this._router.currentRoute)
+ this._router.app = this
+ // @ts-ignore
+ Vue.util.defineReactive(
+ // @ts-ignore
+ this,
+ '_route',
+ // @ts-ignore
+ this._router.currentRoute
+ // undefined,
+ // true
+ )
} else {
// @ts-ignore
this._routerRoot = (this.$parent && this.$parent._routerRoot) || this
private beforeGuards: NavigationGuard[] = []
private afterGuards: PostNavigationGuard[] = []
currentRoute: Readonly<RouteLocationNormalized> = START_LOCATION_NORMALIZED
+ private app: any
constructor(options: RouterOptions) {
this.history = options.history
...to,
...matchedRoute,
}
+ this.updateReactiveRoute()
} catch (error) {
- // TODO: use the push/replace techieque with any navigation to
+ // TODO: use the push/replace technique with any navigation to
// preserve history when moving forward
if (error instanceof NavigationGuardRedirect) {
this.push(error.to)
// TODO: should we throw an error as the navigation was aborted
// TODO: needs a proper check because order could be different
- if (this.currentRoute.fullPath === url.fullPath) return this.currentRoute
+ if (
+ this.currentRoute !== START_LOCATION_NORMALIZED &&
+ this.currentRoute.fullPath === url.fullPath
+ )
+ return this.currentRoute
const toLocation: RouteLocationNormalized = location
// trigger all guards, throw if navigation is rejected
const from = this.currentRoute
this.currentRoute = toLocation
+ this.updateReactiveRoute()
// navigation is confirmed, call afterGuards
for (const guard of this.afterGuards) guard(toLocation, from)
if (i > -1) this.afterGuards.splice(i, 1)
}
}
+
+ private updateReactiveRoute() {
+ if (!this.app) return
+ this.app._route = this.currentRoute
+ }
}