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. indy idhttpserver有关下载的两个问题

    http://aawwmate.blog.163.com/blog/static/77528256201092733950315/ indy idhttpserver有关下载的两个问题 2010-10 ...

  2. groub by 与 over partition by 的区别

    这个逻辑,写的很对.明白了这个意思. over partition by 前面一定要用汇总函数.groub by 就可以不用.本质都是汇总 SELECT a.* ,SUM(a.audit_status ...

  3. 2019/11/02 TZOJ

    1001 ShaoLin http://www.tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=6003 标记一下i ...

  4. 有趣的linux指令

    1.cmatrix sudo apt-get update sudo apt-get install cmatrix 2.asciiquarium wget http://search.cpan.or ...

  5. 08 (h5*) js第9天--原型、继承

    目录: 1:原型和原型链 2:构造函数的原型可以改变 3:原型的最终指向 4:先修改原型指向,在添加方法. 5:实例对象中的属性和原型属性重合, 6:一个神奇的原型链 7:继承 8:原型链 9:利用c ...

  6. pipenv虚拟环境

    虚拟环境 之前用的 virtualenv +virtualenvwrapper 今天在学习  flask 框架    用到了pipenv pipenv   Pipfile 文件是 TOML 格式而不是 ...

  7. Blocks题解(区间dp)

    Blocks题解 区间dp 阅读体验...https://zybuluo.com/Junlier/note/1289712 很好的一道区间dp的题目(别问我怎么想到的) dp状态 其实这个题最难的地方 ...

  8. PHP实现上传文件到服务器

    <?php /**************************** *** 功能:上传文件到服务器 ****************************/ session_start() ...

  9. cdn.bootcss.com无法访问 解决方法

    今天angularjs的网站突然加载报错,提示Refused to execute script from 'https://cdnjs.com/' because its MIME type ('t ...

  10. 【学习总结】gcc和gdb

    目录 <> vim.gcc.gdb: gcc: gcc和g++是c/c++的linux系统集成的编译器,源文件的后缀应为 .C/.cpp/.c++/.cc等 编译器可以将C.C++等语言源 ...