二、404 错误
hash模式下,仅hash符号之前的内容会被包含在请求中,如http://www.abc.com, 因此对于后端来说,即使没有做到对路由的全覆盖,也不会返回404错误;history模式下,前端的url必须和实际向后端发起请求的url一致,如http://www.abc.com/book/id。如果后端缺少对/book/id的路由处理,将返回404错误。
三、延伸阅读
vue-router实例化如下:
const router = new VueRouter({
mode: "XXX",
routes: [...]
})
VueRouter实例化如下:
export default class VueRouter {
mode: string; // 传入的字符串参数,指示history类别
history: HashHistory | HTML5History | AbstractHistory; // 实际起作用的对象属性,必须是以上三个类的枚举
fallback: boolean; // 如浏览器不支持,'history'模式需回滚为'hash'模式
constructor (options: RouterOptions = {}) {
let mode = options.mode || 'hash' // 默认为'hash'模式
this.fallback = mode === 'history' && !supportsPushState // 通过supportsPushState判断浏览器是否支持'history'模式
if (this.fallback) {
mode = 'hash'
}
if (!inBrowser) {
mode = 'abstract' // 不在浏览器环境下运行需强制为'abstract'模式
}
this.mode = mode
// 根据mode确定history实际的类并实例化
switch (mode) {
case 'history':
this.history = new HTML5History(this, options.base)
break
case 'hash':
this.history = new HashHistory(this, options.base, this.fallback)
break
case 'abstract':
this.history = new AbstractHistory(this, options.base)
break
default:
if (process.env.NODE\_ENV !== 'production') {
assert(false, `invalid mode: ${mode}`)
}
}
}
// 初始化操作, app表示组件实例
init (app: any /\* Vue component instance \*/) {
// ...
this.apps.push(app)
// main app already initialized.
if (this.app) {
return
}
this.app = app
const history = this.history
// 根据history的类别执行相应的初始化操作和监听
if (history instanceof HTML5History) {
// html5模式
history.transitionTo(history.getCurrentLocation())
} else if (history instanceof HashHistory) {
// hash模式
const setupHashListener = () => {
history.setupListeners()
}
history.transitionTo(
history.getCurrentLocation(),
setupHashListener,
setupHashListener
)
}
// 切换路由
history.listen(route => {
this.apps.forEach((app) => {
app._route = route
})
})
}
// 一些钩子函数 beforeEach、 afterEach等
// ...
// ...
// VueRouter类暴露的以下方法实际是调用具体history对象的方法
push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
this.history.push(location, onComplete, onAbort)
}
replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
this.history.replace(location, onComplete, onAbort)
}
}
// 相对于当前页面向前或向后跳转多少个页面,类似 window.history.go(n)。n可为正数可为负数。正数返回上一个页面
go (n: number) {
this.history.go(n)
}
// 后退到上一个页面
back () {
this.go(-1)
}
// 前进到下一个页面
forward () {
this.go(1)
}
// ...
// ...
代码解读:
- 浏览器默认是
hash模式,当浏览器不支持html5的history模式时,也会强制为hash模式;当环境不是浏览器时,强制为abstract模式。 - 当创建
VueRouter实例后,VueRouter构造函数会通过传入对象options的mode参数,来调用对应的(HashHistory / HTML5History / AbstractHistory)构造函数,进而创建对应的history实例对象。 - 创建相应的
history实例后,可以看到init函数里面会有对应的初始化操作。
$router实例有两个常见的跳转方法:push与replace方法,源码的最下面,就暴露出来这两个方法。很显然这两种方法都是对不同模式下的方法的封装,本质还是执行的对应模式上的方法。
HashHistory中的push()方法:
push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
// 执行transitionTo函数
this.transitionTo(location, route => {
// 改变hash值
pushHash(route.fullPath)
onComplete && onComplete(route)
}, onAbort)
}
function pushHash (path) {
window.location.hash = path
}
pushHash直接对window.location.hash赋值,hash值变化之后,浏览器访问历史中就会增加一个记录。
记录增加了,如何更新视图呢?接着看父类History中transitionTo函数是如何实现的。
transtitionTo函数具体实现如下:
transitionTo (location: RawLocation, onComplete?: Function, onAbort?: Function) {
//找到匹配路由(match函数)
const route = this.router.match(location, this.current)
this.confirmTransition(route, () => { //确认是否转化
this.updateRoute(route) //更新route
// ...
})
}
//更新路由
updateRoute (route: Route) {
const prev = this.current // 跳转前路由
this.current = route // 准备跳转路由
this.cb && this.cb(route) // 回调函数,这一步很重要,这个回调函数在index文件中注册,会更新被劫持的数据 \_router
this.router.afterHooks.forEach(hook => {
hook && hook(route, prev)
})
}
}
// this.cb函数的定义
listen (cb: Function) {
this.cb = cb
}
在transitionTo函数中,执行了updateRoute更新路由函数,这个函数中执行了cb这个函数,cb这个函数是在VueRouter实例中的init函数中通过history.listen传入的。
init (app: any /\* Vue component instance \*/) {
// ...
this.apps.push(app)
// ...
history.listen(route => {
this.apps.forEach((app) => {
app._route = route
})
})
}
上面的代码,从$router.push 分析到了cb()函数, 再接着看cb函数,这个函数中有一个_route属性,并且将匹配的路由route赋给了app._route。(这里app是组件实例,apps是所有组件实例一个数组) 。
那么_route属性是什么,组件本身是没有定义这个属性的,那么这个属性从哪里来的呢?源码中在install()方法中使用Vue.mixin()方法添加一个全局的混合对象:
install.js代码里面做了四件事:
- 混入
beforeCreate函数,在里面定义_route这个响应式属性。 - 混入
destroyed函数。 - 在
Vue.prototype上定义$router、$route这两个对象,以便每个组件都可以获得。 - 在Vue
上注册router-link与router-view这两个组件。
// install.js代码:
export function install (Vue) {
if (install.installed && _Vue === Vue) return
install.installed = true
_Vue = Vue
const isDef = v => v !== undefined
const registerInstance = (vm, callVal) => {
let i = vm.$options._parentVnode
if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
i(vm, callVal)
}
}
// 使用mixin会在每个.vue文件中进行beforeCreate和destroyed
Vue.mixin({
//对每个Vue实例混入beforeCreate钩子操作
//验证Vue实例是否有router对象了,如果有,就不再初始化了
beforeCreate () {
// 如果没有router对象(this.$option.router来自于VueRouter实例router对象)
if (isDef(this.$options.router)) {
// 将\_routerRoot指向当前组件
this._routerRoot = this
// 将router对象挂载到根组件的\_router上

被折叠的 条评论
为什么被折叠?



