Vue-Router原理
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原理的更多相关文章
- 「进阶篇」Vue Router 核心原理解析
前言 此篇为进阶篇,希望读者有 Vue.js,Vue Router 的使用经验,并对 Vue.js 核心原理有简单了解: 不会大篇幅手撕源码,会贴最核心的源码,对应的官方仓库源码地址会放到超上,可以配 ...
- vue路由原理剖析
单页面应用(SPA)的核心之一是: 更新视图而不重新请求页面, 实现这一点主要是两种方式: 1.Hash: 通过改变hash值 2.History: 利用history对象新特性(详情可出门左拐见: ...
- 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 ...
- 深入浅出的webpack构建工具--webpack4+vue+router项目架构(十四)
阅读目录 一:vue-router是什么? 二:vue-router的实现原理 三:vue-router使用及代码配置 四:理解vue设置路由导航的两种方法. 五:理解动态路由和命名视图 六:理解嵌套 ...
- vue router的浏览器跳转行为
最近做的项目中,涉及vue router 路由操作,其操作方法不同,产生的行为亦不同.本文通过对比实验,对其行为进行实验对比及总结,避免混淆. vue router的单页跳转的history模式,类似 ...
- framework7的改进,以及与vue组合使用遇到的问题以及解决方法 (附vue的原理)
framework7官方提供了vue+framework7的组合包,但是那个包用起来复杂度较高,而且不灵活.听说bug也不少. 所以我想用最原始的方式单独使用vue和framework7. 遇到以下问 ...
- [Vue 牛刀小试]:第十二章 - 使用 Vue Router 实现 Vue 中的前端路由控制
一.前言 前端路由是什么?如果你之前从事的是后端的工作,或者虽然有接触前端,但是并没有使用到单页面应用的话,这个概念对你来说还是会很陌生的.那么,为什么会在单页面应用中存在这么一个概念,以及,前端路由 ...
- [Vue 牛刀小试]:第十三章 - Vue Router 基础使用再探(命名路由、命名视图、路由传参)
一.前言 在上一章的学习中,我们简单介绍了前端路由的概念,以及如何在 Vue 中通过使用 Vue Router 来实现我们的前端路由.但是在实际使用中,我们经常会遇到路由传参.或者一个页面是由多个组件 ...
- 04 Vue Router路由管理器
路由的基本概念与原理 Vue Router Vue Router (官网: https://router.vuejs.org/zh/)是Vue.js 官方的路由管理器. 它和vue.js的核心深度集成 ...
- Vue 2.0 + Vue Router + Vuex
用 Vue.js 2.x 与相配套的 Vue Router.Vuex 搭建了一个最基本的后台管理系统的骨架. 当然先要安装 node.js(包括了 npm).vue-cli 项目结构如图所示: ass ...
随机推荐
- springboot参数校验
为了能够进行嵌套验证,必须手动在Item实体的props字段上明确指出这个字段里面的实体也要进行验证.由于@Validated不能用在成员属性(字段)上,但是@Valid能加在成员属性(字段)上,而且 ...
- ModuleNotFoundError: No module named 'mysql'
问题:使用sqlalchemy初始化数据库报错,如图: 解决办法: pip install mysql-connector 安装后即可正常运行了 参照: https://blog.csdn.net/j ...
- php面向对象三大特性
1.封装: 目的:使类更加安全 步骤:1.成员变量变成private(私有的)2.设置方法/调用方法3.在方法中增加限制 <?php class shao { private $aa;//必须是 ...
- Installing Symfony project with PHP 7.3 version
参考地址:https://cmsdk.com/php/installing-symfony-project-with-php-7-3-version.html Tryng to install (an ...
- tensorflow 之Dataset数据集之批量数据
###生成批次数据 import tensorflow as tf '''reapt()生成重复数据集 batch()将数据集按批次组合''' file_name = ['img1','img2',' ...
- Vue-实现简单拖拽(自定义属性)
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...
- vim-tabe多标签切换
vim-tabe多标签切换 本文转载自https://www.cnblogs.com/liqiu/archive/2013/03/26/2981949.html 1.新建标签页 使用:tabe命令和文 ...
- debain8 安装mysql8
一.下载apt源 https://dev.mysql.com/downloads/repo/apt/ 二.更新apt sudo apt-get update 三.安装mysql sudo apt-ge ...
- no suitable HttpMessageConverter found for response type
在使用 RestTemplate 或者 Spring Cloud 的时候,经常会出现这个错误. 基本上出现的场景都是,我们要json,结果来了个 text/html. 两个原因: 第一个就是:服务器返 ...
- emacs配置&博客界面源代码
emacs配置 如果想要考场简单配置也可以去下面看,需要别的考场配置可以自己在下面比较全的里面找 考试备忘录(有新的就会更的...) By Junlier (global-set-key [f9] ' ...