路由模式:
hash:
主要靠的是location.hash load事件 hashchange事件
在浏览器中有一个api叫location,即hash路由的原理是location.hash
当html加载完毕后会自动触发Load事件
当hash改变时会触发hashchange事件
history: 主要靠的是h5的api:history.pushState(里面有三个参数{},null,pathname) popsate(监听URL的变化)
export default new VueRouter({
mode:'history',
routes
})
首先定义一个VueRouter类,并在里面创建一个constructor,并把这个类导出去
constructor会指向创建这个对象的函数,里面定义的变量都会挂在VueRouter实例上,得到路由模式、规则
在VueRouter上挂载install方法,install方法的第一个参数就是Vue构造器,使用Vue.use(Vue-router)时会调用install方法
利用mixin机制混入beforeCreate钩子函数,访问根组件时调用,获取根组件,把实例挂载到_root上面 让$router在全局中可以使用, defineProperty(obj,prop,descriptor), obj要定义的属性的对象;prop要定义或修改的对象的属性,descriptor:要定义或修改的属性值