update
This commit is contained in:
+78
-46
@@ -2,12 +2,13 @@
|
||||
|
||||
import { install } from './install'
|
||||
import { START } from './util/route'
|
||||
import { assert } from './util/warn'
|
||||
import { assert, warn } from './util/warn'
|
||||
import { inBrowser } from './util/dom'
|
||||
import { cleanPath } from './util/path'
|
||||
import { createMatcher } from './create-matcher'
|
||||
import { normalizeLocation } from './util/location'
|
||||
import { supportsPushState } from './util/push-state'
|
||||
import { handleScroll } from './util/scroll'
|
||||
|
||||
import { HashHistory } from './history/hash'
|
||||
import { HTML5History } from './history/html5'
|
||||
@@ -15,24 +16,32 @@ import { AbstractHistory } from './history/abstract'
|
||||
|
||||
import type { Matcher } from './create-matcher'
|
||||
|
||||
export default class VueRouter {
|
||||
static install: () => void;
|
||||
static version: string;
|
||||
import { isNavigationFailure, NavigationFailureType } from './util/errors'
|
||||
|
||||
app: any;
|
||||
apps: Array<any>;
|
||||
ready: boolean;
|
||||
readyCbs: Array<Function>;
|
||||
options: RouterOptions;
|
||||
mode: string;
|
||||
history: HashHistory | HTML5History | AbstractHistory;
|
||||
matcher: Matcher;
|
||||
fallback: boolean;
|
||||
beforeHooks: Array<?NavigationGuard>;
|
||||
resolveHooks: Array<?NavigationGuard>;
|
||||
afterHooks: Array<?AfterNavigationHook>;
|
||||
export default class VueRouter {
|
||||
static install: () => void
|
||||
static version: string
|
||||
static isNavigationFailure: Function
|
||||
static NavigationFailureType: any
|
||||
static START_LOCATION: Route
|
||||
|
||||
app: any
|
||||
apps: Array<any>
|
||||
ready: boolean
|
||||
readyCbs: Array<Function>
|
||||
options: RouterOptions
|
||||
mode: string
|
||||
history: HashHistory | HTML5History | AbstractHistory
|
||||
matcher: Matcher
|
||||
fallback: boolean
|
||||
beforeHooks: Array<?NavigationGuard>
|
||||
resolveHooks: Array<?NavigationGuard>
|
||||
afterHooks: Array<?AfterNavigationHook>
|
||||
|
||||
constructor (options: RouterOptions = {}) {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
warn(this instanceof VueRouter, `Router must be called with the new operator.`)
|
||||
}
|
||||
this.app = null
|
||||
this.apps = []
|
||||
this.options = options
|
||||
@@ -42,7 +51,8 @@ export default class VueRouter {
|
||||
this.matcher = createMatcher(options.routes || [], this)
|
||||
|
||||
let mode = options.mode || 'hash'
|
||||
this.fallback = mode === 'history' && !supportsPushState && options.fallback !== false
|
||||
this.fallback =
|
||||
mode === 'history' && !supportsPushState && options.fallback !== false
|
||||
if (this.fallback) {
|
||||
mode = 'hash'
|
||||
}
|
||||
@@ -68,11 +78,7 @@ export default class VueRouter {
|
||||
}
|
||||
}
|
||||
|
||||
match (
|
||||
raw: RawLocation,
|
||||
current?: Route,
|
||||
redirectedFrom?: Location
|
||||
): Route {
|
||||
match (raw: RawLocation, current?: Route, redirectedFrom?: Location): Route {
|
||||
return this.matcher.match(raw, current, redirectedFrom)
|
||||
}
|
||||
|
||||
@@ -81,11 +87,12 @@ export default class VueRouter {
|
||||
}
|
||||
|
||||
init (app: any /* Vue component instance */) {
|
||||
process.env.NODE_ENV !== 'production' && assert(
|
||||
install.installed,
|
||||
`not installed. Make sure to call \`Vue.use(VueRouter)\` ` +
|
||||
`before creating root instance.`
|
||||
)
|
||||
process.env.NODE_ENV !== 'production' &&
|
||||
assert(
|
||||
install.installed,
|
||||
`not installed. Make sure to call \`Vue.use(VueRouter)\` ` +
|
||||
`before creating root instance.`
|
||||
)
|
||||
|
||||
this.apps.push(app)
|
||||
|
||||
@@ -99,11 +106,7 @@ export default class VueRouter {
|
||||
// we do not release the router so it can be reused
|
||||
if (this.app === app) this.app = this.apps[0] || null
|
||||
|
||||
if (!this.app) {
|
||||
// clean up event listeners
|
||||
// https://github.com/vuejs/vue-router/issues/2341
|
||||
this.history.teardownListeners()
|
||||
}
|
||||
if (!this.app) this.history.teardown()
|
||||
})
|
||||
|
||||
// main app previously initialized
|
||||
@@ -117,14 +120,28 @@ export default class VueRouter {
|
||||
const history = this.history
|
||||
|
||||
if (history instanceof HTML5History || history instanceof HashHistory) {
|
||||
const setupListeners = () => {
|
||||
history.setupListeners()
|
||||
const handleInitialScroll = routeOrError => {
|
||||
const from = history.current
|
||||
const expectScroll = this.options.scrollBehavior
|
||||
const supportsScroll = supportsPushState && expectScroll
|
||||
|
||||
if (supportsScroll && 'fullPath' in routeOrError) {
|
||||
handleScroll(this, routeOrError, from, false)
|
||||
}
|
||||
}
|
||||
history.transitionTo(history.getCurrentLocation(), setupListeners, setupListeners)
|
||||
const setupListeners = routeOrError => {
|
||||
history.setupListeners()
|
||||
handleInitialScroll(routeOrError)
|
||||
}
|
||||
history.transitionTo(
|
||||
history.getCurrentLocation(),
|
||||
setupListeners,
|
||||
setupListeners
|
||||
)
|
||||
}
|
||||
|
||||
history.listen(route => {
|
||||
this.apps.forEach((app) => {
|
||||
this.apps.forEach(app => {
|
||||
app._route = route
|
||||
})
|
||||
})
|
||||
@@ -193,11 +210,14 @@ export default class VueRouter {
|
||||
if (!route) {
|
||||
return []
|
||||
}
|
||||
return [].concat.apply([], route.matched.map(m => {
|
||||
return Object.keys(m.components).map(key => {
|
||||
return m.components[key]
|
||||
return [].concat.apply(
|
||||
[],
|
||||
route.matched.map(m => {
|
||||
return Object.keys(m.components).map(key => {
|
||||
return m.components[key]
|
||||
})
|
||||
})
|
||||
}))
|
||||
)
|
||||
}
|
||||
|
||||
resolve (
|
||||
@@ -213,12 +233,7 @@ export default class VueRouter {
|
||||
resolved: Route
|
||||
} {
|
||||
current = current || this.history.current
|
||||
const location = normalizeLocation(
|
||||
to,
|
||||
current,
|
||||
append,
|
||||
this
|
||||
)
|
||||
const location = normalizeLocation(to, current, append, this)
|
||||
const route = this.match(location, current)
|
||||
const fullPath = route.redirectedFrom || route.fullPath
|
||||
const base = this.history.base
|
||||
@@ -233,7 +248,21 @@ export default class VueRouter {
|
||||
}
|
||||
}
|
||||
|
||||
getRoutes () {
|
||||
return this.matcher.getRoutes()
|
||||
}
|
||||
|
||||
addRoute (parentOrRoute: string | RouteConfig, route?: RouteConfig) {
|
||||
this.matcher.addRoute(parentOrRoute, route)
|
||||
if (this.history.current !== START) {
|
||||
this.history.transitionTo(this.history.getCurrentLocation())
|
||||
}
|
||||
}
|
||||
|
||||
addRoutes (routes: Array<RouteConfig>) {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
warn(false, 'router.addRoutes() is deprecated and has been removed in Vue Router 4. Use router.addRoute() instead.')
|
||||
}
|
||||
this.matcher.addRoutes(routes)
|
||||
if (this.history.current !== START) {
|
||||
this.history.transitionTo(this.history.getCurrentLocation())
|
||||
@@ -256,6 +285,9 @@ function createHref (base: string, fullPath: string, mode) {
|
||||
|
||||
VueRouter.install = install
|
||||
VueRouter.version = '__VERSION__'
|
||||
VueRouter.isNavigationFailure = isNavigationFailure
|
||||
VueRouter.NavigationFailureType = NavigationFailureType
|
||||
VueRouter.START_LOCATION = START
|
||||
|
||||
if (inBrowser && window.Vue) {
|
||||
window.Vue.use(VueRouter)
|
||||
|
||||
Reference in New Issue
Block a user