this.go(1)
}
getMatchedComponents (to?: RawLocation | Route): Array<any> {
const route: any = to
? to.matched
? to
: this.resolve(to).route
: this.currentRoute
if (!route) {
return [] }
return [].concat.apply([], route.matched.map(m => {
return Object.keys(m.components).map(key => {
return m.components[key] })
}))
}
resolve (
to: RawLocation,
current?: Route,
append?: boolean
): {
location: Location,
route: Route,
href: string,
// for backwards compat
normalizedTo: Location,
resolved: Route
} {
const location = normalizeLocation(
to,
current || this.history.current,
append,
this
)
const route = this.match(location, current)
const fullPath = route.redirectedFrom || route.fullPath
const base = this.history.base
const href = createHref(base, fullPath, this.mode)
return {
location,
route,
href,
// for backwards compat
normalizedTo: location,
resolved: route
}
}
addRoutes (routes: Array<RouteConfig>) {
this.matcher.addRoutes(routes)
if (this.history.current !== START) {
this.history.transitionTo(this.history.getCurrentLocation())
}
}
}
HashHistory
• hash虽然出现在url中,但不会被包括在http请求中,它是用来指导浏览器动作的,对服务器端没影响,因此,改变hash不会重新加载页面。
• 可以为hash的改变添加监听事件:
window.addEventListener("hashchange",funcRef,false)• 每一次改变hash(window.location.hash),都会在浏览器访问历史中增加一个记录。
export class HashHistory extends History {
constructor (router: Router, base: ?string, fallback: boolean) {
super(router, base)
// check history fallback deeplinking
// 如果是从history模式降级来的,需要做降级检查
if (fallback && checkFallback(this.base)) {
// 如果降级且做了降级处理,则返回
return
}
ensureSlash()
}
.......function checkFallback (base) {
const location = getLocation(base)
// 得到除去base的真正的 location 值
if (!/^/#/.test(location)) {
// 如果此时地址不是以 /# 开头的
// 需要做一次降级处理,降为 hash 模式下应有的 /# 开头
window.location.replace(
cleanPath(base + '/#' + location)
)
return true
}
}
function ensureSlash (): boolean {
// 得到 hash 值
const path = getHash()
if (path.charAt(0) === '/') {
// 如果是以 / 开头的,直接返回即可










