From: Eduardo San Martin Morote Date: Thu, 6 Jun 2019 14:19:58 +0000 (+0200) Subject: feat: working version of simple router-view X-Git-Tag: v4.0.0-alpha.0~348 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4f1bab908bf47016cf01470b88d57925452926e9;p=thirdparty%2Fvuejs%2Frouter.git feat: working version of simple router-view --- diff --git a/explorations/html5.html b/explorations/html5.html index 8f31cb5c..c2cd7fb7 100644 --- a/explorations/html5.html +++ b/explorations/html5.html @@ -13,6 +13,7 @@ Cancel Next Navigation + diff --git a/explorations/html5.ts b/explorations/html5.ts index 05f1770e..40c6343e 100644 --- a/explorations/html5.ts +++ b/explorations/html5.ts @@ -15,6 +15,14 @@ const component: RouteComponent = { template: `
A component
`, } +const Home: RouteComponent = { + template: `
Home
`, +} + +const User: RouteComponent = { + template: `
User: {{ $route.params.id }}
`, +} + const GuardedWithLeave: RouteComponent = { template: `

try to leave

@@ -28,8 +36,8 @@ const GuardedWithLeave: RouteComponent = { 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 }, { @@ -107,19 +115,19 @@ async function run() { 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({ diff --git a/src/components/View.ts b/src/components/View.ts index 0837e195..23df951a 100644 --- a/src/components/View.ts +++ b/src/components/View.ts @@ -17,6 +17,8 @@ const View: FunctionalComponentOptions = { // @ts-ignore const route = parent.$route + console.log('rendering', route) + // TODO: support nested router-views const matched = route.matched[0] diff --git a/src/index.ts b/src/index.ts index c11583cf..db8c4e6e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,17 @@ const plugin: PluginFunction = Vue => { 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 diff --git a/src/router.ts b/src/router.ts index f5450439..a474513b 100644 --- a/src/router.ts +++ b/src/router.ts @@ -32,6 +32,7 @@ export class Router { private beforeGuards: NavigationGuard[] = [] private afterGuards: PostNavigationGuard[] = [] currentRoute: Readonly = START_LOCATION_NORMALIZED + private app: any constructor(options: RouterOptions) { this.history = options.history @@ -53,8 +54,9 @@ export class Router { ...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) @@ -184,7 +186,11 @@ export class Router { // 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 @@ -205,6 +211,7 @@ export class Router { const from = this.currentRoute this.currentRoute = toLocation + this.updateReactiveRoute() // navigation is confirmed, call afterGuards for (const guard of this.afterGuards) guard(toLocation, from) @@ -323,4 +330,9 @@ export class Router { if (i > -1) this.afterGuards.splice(i, 1) } } + + private updateReactiveRoute() { + if (!this.app) return + this.app._route = this.currentRoute + } }