当前位置: 首页 > news >正文

档案网站建设图片云虚拟主机可以做视频网站不

档案网站建设图片,云虚拟主机可以做视频网站不,公司网站怎么设计,在线音乐网站开发数据库这是对vue-router 3 版本的源码分析。 本次分析会按以下方法进行: 按官网的使用文档顺序,围绕着某一功能点进行分析。这样不仅能学习优秀的项目源码,更能加深对项目的某个功能是如何实现的理解。这个对自己的技能提升,甚至面试时…

这是对vue-router 3 版本的源码分析。
本次分析会按以下方法进行:

  1. 按官网的使用文档顺序,围绕着某一功能点进行分析。这样不仅能学习优秀的项目源码,更能加深对项目的某个功能是如何实现的理解。这个对自己的技能提升,甚至面试时的回答都非常有帮助。
  2. 在围绕某个功能展开讲解时,所有不相干的内容都会暂时去掉,等后续涉及到对应的功能时再加上。这样最大的好处就是能循序渐进地学习,同时也不会被不相干的内容影响。省略的内容都会在代码中以…表示。
  3. 每段代码的开头都会说明它所在的文件目录,方便定位和查阅。如果一个函数内容有多个函数引用,这些都会放在同一个代码块中进行分析,不同路径的内容会在其头部加上所在的文件目录。

本章讲解router中重定向是如何实现的。
另外我的vuex3源码分析也发布完了,欢迎大家学习:
vuex3 最全面最透彻的源码分析
还有vue-router的源码分析:
vue-router 源码分析——1. 路由匹配
vue-router 源码分析——2. router-link 组件是如何实现导航的
vue-router 源码分析——3. 动态路由匹配
vue-router 源码分析——4.嵌套路由
vue-router 源码分析——5.编程式导航
vue-router 源码分析——6.命名路由
vue-router 源码分析——7.命名视图

官方示例:

  • 重定向是通过 routes 配置来完成,重定向的目标也可以是一个命名路由,或者一个方法。
const router = new VueRouter({routes: [{ path: '/a', redirect: '/b' },{ path: '/b', component: b_component} // 重定向的路由必须在router中定义]
})const router = new VueRouter({routes: [{ path: '/a', redirect: { name: 'foo' }}]
})const router = new VueRouter({routes: [{ path: '/a', redirect: to => {// 方法接收 目标路由 作为参数// return 重定向的 字符串路径/路径对象}}]
})

Router初始化:

  • Router初始化生成匹配器和路由记录表,redirect属性会被复制到路由的路由记录上:
// router.js
import type { Matcher } from './create-matcher'
export default class VueRouter {...constructor(options: RouterOptions = {}) {...this.matcher = createMatcher(options.routes || [], this)}
}// ./create-matcher.js
import { createRouteMap } from './create-route-map'
export function createMatcher(routes: Array<RouteConfig>,router: VueRouter
): Matcher {const { pathList, pathMap, nameMap } = createRouteMap(routes)...
}// ./create-route-map/js
export function createRouteMap(routes: Array<RouteConfig>,...
): {pathList: Array<string>,pathMap: Dictionary<RouteRecord>,nameMap: Dictionary<RouteRecord>
} {const pathList: Array<string> =  []const pathMap: Dictionary<RouteRecord> = Object.create(null)const nameMap: Dictionary<RouteRecord> = Object.create(null)routes.forEach(route => {addRouteRecord(pathList, pathMap, nameMap, route, parentRoute)})...
}function addRouteRecord(pathList: Array<string>,pathMap: Dictionary<RouteRecord>,nameMap: Dictionary<RouteRecord>,route: RouteConfig,parent?: RouteRecord,matchAs?: string
) {...const record: RouteRecord = {redirect: route.redirect,...    }
}

触发逻辑:

  • 当试图跳转到url ‘/a’ 时,会触发路由匹配。
  • 首先匹配到路由路径为’/a’的路由记录,然后判断它是否有redirect属性,如果有就执行redirect函数。
  • 接着在redirect函数中取出redirect内容,对其进行了结构类型统一化。这里就解释了为什么配置重定向的目标可以是字符串形式的path,或者命名路由,或者方法。
  • 最后,构建了一个基于redirect目标的数据,作为参数传入match函数进行路由匹配。这样的递归调用也能解决多重的重定向。
// create-matcher.js
export function createMatcher(routes: Array<RouteConfig>,router: VueRouter
): Matcher {const { pathList, pathMap, nameMap } = createRouteMap(routes)...function match(raw: RawLocation,currentRoute?: Route,redirectedFrom?: Location     ): Route {const location = normalizeLocation(raw, currentRoute, false, router)const { name } = locationif (name) {// 基于命名路由的匹配逻辑        } else if (location.path) {location.params = {}for (let i = 0; i < pathList.length; i++) {const path = pathList[i]const record = pathMap[path]if (matchRoute(record.regex, location.path, location.params)) {// 这里匹配到路由'/a'的相关记录后,执行创建路由的操作return _createRoute(record, location, redirectedFrom)}}      }}function _createRoute(record: ?RouteRecord,location: Location,redirectedFrom?: Location    ): Route {// 如果当前的路由记录有重定向,就执行重定向的相关逻辑,否则创建'/a'的路由内容if (record && record.redirect) {return redirect(record, redirectedFrom || location)        }...return createRoute(record, location, redirectedFrom, router)}function redirect(record: RouteRecord,location: Location): Route {const originalRedirect = record.redirect// 下面这段代码解释了为什么重定向可以是一个方法,同时接受的参数是目标路由,let redirect = typeof originalRedirect === 'function'? originalRedirect(createRoute(record, location, null, router)): originalRedirect// 最后无论重定向设置的是字符串,命名路由,方法,都会统一成一个对象结构if (typeof redirect === 'string') {redirect = {path = redirect}        }...const re: Object = redirectconst { name, path } = re...if (name) {const targetRecord = nameMap[name]...return match({name,...            }, undefined, location)       } else if (path) {const rawPath = resolveRecordPath(path, record) const resolvedPath = fillParams(rawPath, params, `redirect route with path "${rawPath}"`)return match({path: resolvedPath, // 官网示例中定义的重定向都不复杂,这里的path = '/b'没有任何改变...          }, undefined, location)       }}
}

细节补充:

  • 当重定向完成,匹配路由成功后,调用createRoute函数生成匹配路由时,增加了一个redirectedFrom属性,记录的是它的重定向的源路径。这样可以方便用户查看或者执行其他功能。
// ./util/route.js
export function createRoute(record: ?RouteRecord,location: Location,redirectedFrom?: ?Location,router?: VueRouter
): Route {...const route: Route = {...  }if (redirectedFrom) {route.redirectedFrom = getFullPath(redirectedFrom, stringifyQuery)            }return Object.freeze(route)
}
http://www.yayakq.cn/news/786912/

相关文章:

  • 网站建设服务合同交印花税吗软件公司招聘网站
  • 盐城网站优化服务大气扁平网站
  • 目前做哪些网站能致富学院网站设计说明书
  • 苏州网站建设与网络推广小程序解决方案网页模板下载
  • 金融投资网站模板湖口网站建设
  • 客户网站建设完成后需要什么网站建设公司muyunke
  • 泸州建设厅官方网站深圳百度推广seo公司
  • 爱网站网站查询会员登录管理系统
  • 安徽省住房城乡建设厅网站官网微山网站建设哪家便宜
  • 怎样做网站ppt做游戏网站思想步骤
  • 网站开发外包维护合同范本外卖在家做咋上网站
  • 做外贸好的网站wordpress 下载弹窗
  • 厦门做个网站多少钱定制开发erp系统
  • 网站建设实例大制作搜狐新闻手机网
  • 如何查询企业有没有做网站免费做片头的网站
  • 做网站彩票的代理好吗深圳著名设计公司
  • 搜索网站怎么做女生适合前端还是后端
  • 鞋子的网站策划方案模板怎么优化网站关键词
  • 毕业设计做网站怎么做wordpress设置恢复
  • app网站开发书籍下载欧美做爰爰爰爰网站
  • 国外可以做自媒体的网站大同建设银行保安招聘网站
  • 网站开发人员分工邢台高端网站建设公司
  • 温州网站托管wordpress 动态特效
  • 江西鄱阳专业做网站第一次和两个老头做网站
  • 朔城网站制作望野思想感情
  • 西双版纳建设局网站旅行网站定制公司
  • 温州手机网站建设宝塔wordpress 数据库
  • 手机在线网站网站建设公司如何进行工作
  • 网站备案 资讯网站建设谁家好
  • 有什么做C语言的网站php网站环境配置