Hash 与 History

路由原理

实现路由

/**
* 1、前端路由与后端路由的区别
后端路由: 输入url => 请求发送到服务器 => 服务器解析请求路径 => 拿到对应页面 => 返回出去
前端路由: 输入url => js解析地址 => 找到对应地址页面 => 执行页面生成的js => 看到页面
区别:
1、前端路由不用发请求,直接在浏览器访问解析;
2、前端路由里有hash
*/ /**
* 2、Hash和History
hash:
1、址栏里出现#,表示hash,#后面就是hash内容;
2、可以通过location.hash拿到;
3、可以通过onhashchange监听hash改变;
特点: hash模式下不会发送请求;
history:
1、history即正常地址栏路径;
2、location.pathname获取路径;
3、可以用onpopstate监听history变化;
特点:
*/ /**
* 3、vue插件基础知识
1、Vue.use(参数) => 使用一个插件;
如果参数为一个方法,则会被执行;
但是无论参数为任何东西,只要有install属性,则优先会执行install方法;
2、ue.mixin() => 往vue全局中混入属性,可以将一些常用方法注入到vue中,通过this可以访问到;
核心是用来自定义生命周期
var a = function(){
console.log('aaa');
}
a.install = function(){
console.log('install');
vue.mixin({
data: ()=>{
return b: 1111 //在vue整个项目中都能访问到b这个属性
}
})
}
Vue.use(a); //会打印出install
*/ /**
* 4、vue源码分析
1、Vue.util //表示vue中工具类
共有四个:
warn
extend
mergeOptions
defineReactive
vue.extend(); //
vue.util.extend(); //浅拷贝
*/ /** 5、手写router插件*/
class HistoryRoute{
constructor (){
this.current = null;
}
}
//options 表示 new Routers里传入的值
class vueRouter {
constructor (options){
this.mode = options.mode || 'hash';
this.routers = options.routers || [];
this.history = new HistoryRoute;
this.routersMap = this.creatMap(this.routers);
this.init();
}
init(){
if(this.mode == 'hash'){
//自动加#
location.hash ? '' : location.hash = '/';
window.addEventListener('load',() => {
this.history.current = location.hash.slice(1);
})
window.addEventListener('hashchange',() => {
this.history.current = location.hash.slice(1);
})
}
}
creatMap(routers){
return routers.reduce((m,current)=>{
m[current.path] = current.component;
return m;
})
}
} vueRouter.install = function(Vue){
//写插件要注意判断插件是否已经注册
if(vueRouter.install.installed) return;
vueRouter.install.installed = true;
Vue.mixin({
beforeCreate(){
if(this.$options && this.$options.router){
//当前this指向vue实例
this._root = this;
this._router = this.$options.router;
Vue.util.defineReactive(this,'current',this._root.history); //第三个参数作为children,一层层查找
}else{
this._root = this.$parent._root; //如果没有找到,向上查找一直找到为止;
}
}
})
Vue.component('router-view',{
render(h){
let current = this._self._root._router.history.current;
console.log(current);
let routerMap = this._self._root._router.routersMap;
return h(routerMap[current]);
}
});
} export default vueRouter;

Vue-Router原理的更多相关文章

  1. 「进阶篇」Vue Router 核心原理解析

    前言 此篇为进阶篇,希望读者有 Vue.js,Vue Router 的使用经验,并对 Vue.js 核心原理有简单了解: 不会大篇幅手撕源码,会贴最核心的源码,对应的官方仓库源码地址会放到超上,可以配 ...

  2. vue路由原理剖析

    单页面应用(SPA)的核心之一是: 更新视图而不重新请求页面, 实现这一点主要是两种方式: 1.Hash: 通过改变hash值 2.History: 利用history对象新特性(详情可出门左拐见:  ...

  3. vue router.push(),router.replace(),router.go()和router.replace后需要返回两次的问题

    转载:https://www.cnblogs.com/lwwen/p/7245083.html https://blog.csdn.net/qq_15385627/article/details/83 ...

  4. 深入浅出的webpack构建工具--webpack4+vue+router项目架构(十四)

    阅读目录 一:vue-router是什么? 二:vue-router的实现原理 三:vue-router使用及代码配置 四:理解vue设置路由导航的两种方法. 五:理解动态路由和命名视图 六:理解嵌套 ...

  5. vue router的浏览器跳转行为

    最近做的项目中,涉及vue router 路由操作,其操作方法不同,产生的行为亦不同.本文通过对比实验,对其行为进行实验对比及总结,避免混淆. vue router的单页跳转的history模式,类似 ...

  6. framework7的改进,以及与vue组合使用遇到的问题以及解决方法 (附vue的原理)

    framework7官方提供了vue+framework7的组合包,但是那个包用起来复杂度较高,而且不灵活.听说bug也不少. 所以我想用最原始的方式单独使用vue和framework7. 遇到以下问 ...

  7. [Vue 牛刀小试]:第十二章 - 使用 Vue Router 实现 Vue 中的前端路由控制

    一.前言 前端路由是什么?如果你之前从事的是后端的工作,或者虽然有接触前端,但是并没有使用到单页面应用的话,这个概念对你来说还是会很陌生的.那么,为什么会在单页面应用中存在这么一个概念,以及,前端路由 ...

  8. [Vue 牛刀小试]:第十三章 - Vue Router 基础使用再探(命名路由、命名视图、路由传参)

    一.前言 在上一章的学习中,我们简单介绍了前端路由的概念,以及如何在 Vue 中通过使用 Vue Router 来实现我们的前端路由.但是在实际使用中,我们经常会遇到路由传参.或者一个页面是由多个组件 ...

  9. 04 Vue Router路由管理器

    路由的基本概念与原理 Vue Router Vue Router (官网: https://router.vuejs.org/zh/)是Vue.js 官方的路由管理器. 它和vue.js的核心深度集成 ...

  10. Vue 2.0 + Vue Router + Vuex

    用 Vue.js 2.x 与相配套的 Vue Router.Vuex 搭建了一个最基本的后台管理系统的骨架. 当然先要安装 node.js(包括了 npm).vue-cli 项目结构如图所示: ass ...

随机推荐

  1. MySQL 查询性能优化 - EXPLAIN 命令

    查询优化的官方文档在 这里. EXPLAIN 的输出格式 译文 1. MySQL 架构 1.1 MySQL 的简化架构 MySQL 可以简单的分为三层:连接层.服务层.存储引擎层.其中服务层包含了 M ...

  2. Altium Designer(AD)使用笔记

    在PCB中间打洞,螺丝孔等 制作PCB螺丝孔 1 在Keepout层首先绘制一个圆形(矩形): 2 在绘制PCB时,选中该图形,Tool>>Convert>>create bo ...

  3. 20190825 On Java8 第十三章 函数式编程

    第十三章 函数式编程 函数式编程语言操纵代码片段就像操作数据一样容易. 虽然 Java 不是函数式语言,但 Java 8 Lambda 表达式和方法引用 (Method References) 允许你 ...

  4. 15.队列Queue的特点以及使用,优先级等

    #生产者与消费者模式,模式解释:比如MVC设计模式 ''' 1.队列 (1)特点:先进先出 (2)python2 VS python3 python2:from Queue import queue ...

  5. 爬虫(十一)—— XPath总结

    目录 XPath总结 一.何为XPath 二.XPath语法 1.语法 2.实例 三.XPath轴 1.XPath轴语法 2.XPath轴实例 四.XPath运算符 XPath总结 一.何为XPath ...

  6. PHP学习 fwrite:Warning: fwrite(): supplied argument is not avalid stream resource in

    使用fwrite报错:Warning: fwrite(): supplied argument is not avalid stream resource in 解决方法:文件权限的问题,文件需要77 ...

  7. POJ-1611.TheSuspects.(并查集)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 55832   Accepted: 26501 De ...

  8. [BZOJ2138]stone(Hall定理,线段树)

    Description 话说Nan在海边等人,预计还要等上M分钟.为了打发时间,他玩起了石子.Nan搬来了N堆石子,编号为1到N,每堆 包含Ai颗石子.每1分钟,Nan会在编号在\([L_i,R_i] ...

  9. Java并发编程:线程的同步

    Java并发编程:线程的同步 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} J ...

  10. 73.Largest Rectangle in Histogram(最大矩形)

    Level:   Hard 题目描述: Given n non-negative integers representing the histogram's bar height where the ...