浅析vue-router的三种方法

面试官: 请说一下vue-router的2种方法…

【源码系列】浅析vue-router的三种方法

我: vue-router不是有3种方法吗???源码共享网

一.前语

vuegithub打开私库-r公积金outer到底有几种方法?

【源码系列】浅析vue-router的三种方法

根据vue-route源码下载r官网,咱们能够清楚看到vue公积金借款-routergithub是干什么的的m源码下载ode值有3github是干什么的

hash
h面试问题大全及答案大全istory
abs面试技巧tract

其间,hash 和 history 是 SPA 单页运用程序的基础。

先说结论: spa运用路由有2种方法,hash 和 history,vu面试毛遂自荐一分钟e路由有3种方法,比 spa 多了一个 abstract。

二.源码分析

在vue-router中经过mode这个参数修正路由的方法:

const router =源码超市 new VueRouter({
mode: 'history',
routes: [...]
})

具体怎样完毕的面试问题大全及答案大全面试毛遂自荐3分钟通用,首要咱们下载 vue-router 的源码

抽离出来对m公积金ode面试的处源码下载

class vueRoutgithub打不开gitlaber {
constructGitor(options) {
let面试技巧和注意事项 m源码年代ode = options.mode || 'hash'
this.fallbacgithub中文社区k =
mode === 'hi面试技巧和注意github中文社区事项story' && !supportsPushS面试毛遂自荐tagithub打不开te && options.fallback !源码年代== false
if (this.fallback) {
mode = 'hash'
}
if (!inBrowser) {
mode = 'absgithub打开私库tra源码资本ct'
}
this.mode = mode
switch (mode) {
case 'history':
this.h源码超市istory = new HTML5History(this, o源码之家ptionGitHubs.base)
break
case 'hash':
this.hi面试常见问题及回答技巧story = new HashHistory(this, options.base, this.fallba源码c公积金告贷k)
break
case 'giteeabstract':
this.history = new AbstractHistory(this, options.bGitase)
break
default:
if (process.env.NODE_Egithub永久回家地址NV !== 'production') {
assgithub中文官网网页ert(false, `invalid mode: ${mode}`)
}
}
}
}

能够看到默许运用的是 hash 方法,当设置为 history 时源码编辑器,假设不支持 history 方法,公积金借款也会强制运用 hash 方giti式。
当不在浏览器环境,比方 node 中时,直接giti轮胎是什么品牌强制运用 absgithub直播google渠道永久回家tract 方法。

hash方法

阅览这部分源码前,咱们先来了解下Git hash 的基础:
根据MDN上的介绍,Location 接口的 hash 特征回来一个 USVStrgit教程ing,其间会包括URL标识中的面试技巧 ‘#’ 和 后边URL片段标识符,’#’ 和后边URL片段标识符被称为 hash。
它有这样一些特征:

  1. 在第一个#后边出现的任何字符,都会被浏览器解读为方位标识符。这意味着,这些字符都不会被发源码年代送到服务器端。
  2. 单单改动#后的部分,浏览器只会滚动到相应方位,面试毛遂自荐不会源码同享网从头加载网页。
  3. 每一次改动#后github的部分,都会在浏览器的访问前史中添加一个记载,运用”撤离”按钮,就能够回到上一个源码站方位。
  4. gitee源码下载过window.lo公积金cation.hash特征读取 hash 值,而且源码站 window.locatiogiteengoogle.h面试必问10大问题答复ash 这个特征可读可写。
  5. 运用 window.addEventListene面试毛遂自荐一分钟r(“hashchange”, fun) 能够监听 hash 的改动

了解了这些基本知识后,咱们继续来看 vue-routgithub永久回家地址eGor 源码对 /sr源码怎样做成app软件c/history/hash.js 的处理

    const hand公积金借款leRoutingEvent =面试技巧和注意事项 () =&源码怎样做成app软件gt; {
cons工商银行t current = this.current
if (!ensugiti轮胎是什么品牌reSlash()) {
return
}
this.tran源码sitionTo(getHagithub中文社区sh(), route =>github永久回家地址 {
if (supportsScroll) {
handleScrol宫颈癌疫苗l(thisgithub是干什么的.router, route, current, tgit教程ruegithub永久回git指令家地址)
}
if (!supportsPushState) {
replaceHash(r公积金oute.fullPath)
}
})
}
const evgitiegitintType面试毛遂自荐简略大方 = supporgithub永久回家地址tsPushState ? 'popstate' : 'hashchange'
window.addEventListgithub打不开ener(
eventType,
hangoogledlgit教程eRoutingEvent
)
this源码站.listeners.push(() => {
window.removeEventLigithub打不开stener(eventType, hangithub是干什么的dleRoutingEve公积金告贷nt)
})

首要也是运用 windoGitHubw.addEventListener(“has面试毛遂自荐一分钟hchange”, fun) 监源码站听路由的改动,然后运用 trans源码编辑器itionTo 方法更新视图

  push (location: RawLocation, onComplete?: Function, onAbort?:面试技巧和注意事项 Function) {
const { current: f源码同享网romRoute } = this
this.宫颈癌疫苗transitionTo(
locati宫颈癌疫苗试问题大全及答案大全on,
route => {
pushHash(routegitee.fullPath)
handleScroll(this.r宫颈癌疫苗outer, route, fromRoute, false)
onComplete && onComplete(route)
},Ggit指令it
onAbort
)
}
replace (location: RawLoca工商银github怎样下载文件tion, onComplete?: Funct宫颈癌ion, onAbgithub永久回家地址ort?: FunctGitHubion) {
const { current: fromRoute } = this
this.transitionTo(
location,
route => {
repla源码怎样做成app软件ceHash(github中文官网网页ro面试毛遂自荐简略大方ute.fullPath)
handleScroll(this.ro面试毛遂自荐3分钟通用宫颈癌uter, route, fro面试必问10大问题答复mRoute,面试毛遂自荐一分钟 false)
onComplete && onComplete(route)源码超市
},
onAgiteebgithgithub打不开ugitib中文官网github中文社区网页ort
)
}

vue源码之家-router 的2个首要Agith面试毛遂自荐简略大方ub中文官网网页PI push 和 replace 也是简略处理了下 hash , 然后调用 tra公积金github是干什么的告贷nsitionTo 方法更新视图

hi宫颈面试癌疫github官网story方法

老规矩,先来了解下 HTML5Higithub直面试技巧播渠道永久回家story 的的基本知识:
根据MDN上的介源码之家绍,Historgithub是干什么的y 接口容许操作浏览器的曾经在标签页或许结构里访问的会话前史记载。
运用 back(), forward()和 go() 方法来github中文官网网页完毕在用户前面试问题大全及答案大全史记载中向后和向前的跳转。
HTML5引入了 history.pushState() 和 history.replgithub是干什么的aceState()源码站github永久回家地址面试毛遂自荐简略大方,它们别离能够增源码超市加和修正前史记载条目。
稍微了解下 history.pushState():

window.on面试问题popstate = function(e) {
alert(2);
}
let stateObj = {
foo: "bar",
};
histor面试毛遂自荐面试毛遂自荐3分钟通用y.pushState(stateObj, "page 2", "bar.htmlgithub是干什么的"面试毛遂自荐一分钟);

这将使浏览器地址栏显现为 mozilla.org/bar.html ,但并不会导致浏览器加载 bar.ht狗狗币ml ,乃至不会检查bar.html 是否存在。
也就是说,虽然浏览器 URL 改动了,但不会当即从头向服务端发送央求,这也是 spa运用 更新视图但不 从面试问题大全及答案大全头央求页面的github是干什公积金提取么的基础。
接着咱们继续看 vue-router 源码对 /src/history/html5.js 的处理:

    const handleRoutingEvent = () => {
const current = this.current
// Avoiding first `面试popstate` event dispatchegit指令d in some browsers but first
/github永久回家地址/源码同享网 histor面试问题大全及答案大全y rou狗狗币te not updated sigithub怎样下载文件nce async guard at the same time.
const location = getLocation(this.Gi源码站tHubbase)
if (this.current === START &&源码下载amp; location === this._startLocation) {
retgit指令urn
}
this.transitionTo(locatigit指令on, route => {
if (github官网supp源码之家ortsScroll) {
handleScroll(r狗狗币outer, route, currengiti面试必问10大问题回答轮胎是什么品牌t, true)
}
})
}
window.addEventListener('popstate', handleRoutingEvent)
this.listeners.Ggiti轮胎是什么品牌itpush(() => {
window.removeEGitHubventListener源码同享网('popstate', handleRoutingEvent)
})

处理逻辑和 hash 类似git教程,运用 window.addEventLigithub打不开stener(“popstate”, fun) 监听路由的改动,然后运用github下载 transitionTo 方法更新视图。
push 和 replace 等方法就不再详github直播渠道永久回家细介绍。枸杞

abstract方法

终究github是干什么公积金咱们直接来看一下对 /googlesrc/history/abstract.js 的处理:

  constructor (router: Routegithub打不开r, bgithub是干什么的ase:Git ?strigiti轮胎是什么品牌ng) {
super(公积金借款router, base)
this.stack = []
this.index = -1
}

首要界说了2个变量,stack 来记载调用的记载,源码下载 index 记载当时的指针方位

  push (location: RawLocation, onComplete?: Functiongit指令, ogithub怎公积金提取样下载文件nAbort?: Function) {
this.transitio面试n源码年代Togoogle(
locat面试ion,gitlab
rgitioute => {
this.源码编辑器编程猫下载stack = this面试技巧.stack.github怎样下载文件s公积金licgithub是干什么的e(0, this.公积金index + 1)源码下载.concat(r源码超市oute)
this.index++
onComp源码年代lete && onComplete(rogithub是干什么的ute)源码共享网
},
onAbort
)
}
replace (location: Ragithub打不开wLocatigithub怎样下载文件on, onCgithub永久回家地址omplete?: Function, onAgithub直播渠道永久回家bort?: Function) {
this.transitionTo(
location,
rogithub打不开ute =&gtgithub直播渠道永久回家; {
this.stac枸杞k = this.stack.slic龚俊e(0, this.index).con源码编辑器编程猫下载cat(route)
onComplete &&a面试毛遂自荐简略大方mp; onComplete(route)
},
onAbort
)
}

push 和 re宫颈癌疫苗plac方法 也是经过 stack 和 inde源码之家x 2个变量,模拟出浏览器的前史调用记载。github永久回家地址

三.总结

总算到了终究的总结阶段了:

  1. hash 和 historygithub 的运用方法公积金提取差不多,hash 中路由面试毛遂自荐简略大方带 # ,但是运用简略,不需求服务端协作,站在技术角度讲,这个是配备最简Go略的方法,自己感觉这也是公积金 hash 被设为默许方法的原因
  2. histo公积金提取ry 方法需求服务端协作处理404的状况,但是路由中不带 # ,比 hash 漂亮一点。
  3. abstract 方法没有运用浏览器api,能够放到node环境或许桌面运用中git指令, 自己感觉是对 spa运用 的兜底和才调扩展。

假设有错误或许不谨慎的当地,面试问题请有必要给予纠正,非常感谢。假设喜欢或许有所启示,欢迎star gi宫颈癌疫苗thub ,对作者也是一种煽动。龚俊