(精华)2020年7月19日 vue vue-router(手写版)
发布日期:2021-06-29 15:08:02
浏览次数:3
分类:技术文章
本文共 3571 字,大约阅读时间需要 11 分钟。
import Vue from 'vue'// import VueRouter from 'vue-router'import VueRouter from '../MYRouter/index.js'Vue.use(VueRouter)// 执行install方法const routes = [ { path:'/', redirect:'/goods' }, { path: '/goods', name: 'goods', component: () => import('../views/goods.vue') }, { path: '/ratings', name: 'ratings', component: () => import('../views/ratings.vue') }, { path: '/seller', name: 'seller', component: () => import('../views/seller.vue') },]//new执行构造函数const router = new VueRouter({ routes, linkActiveClass:'active'})export default router
自定义插件MYRouter的内部写法
//我们写插件的地方let Vue ;class MYRouter { static install(_Vue) { //use执行的函数 Vue = _Vue; Vue.mixin({ beforeCreate(){ // const options = this.$options; //this -- vue实例化对象 Vue.prototype.$rmrouter = '我是路由!' if(this.$options.router){ //this.$options.router 在vue实例化的时候被注入的router对象 this._router = this.$options.router Vue.util.defineReactive(this._router, 'current', '/') ; //方式二 //这个是把_route加入数据监控,所以你可以watch到_route this.$options.router.init(); } } }) } constructor(options){ this.$options = options; this.routerMap = { }; // this.currentPath = ''; //当前的path // 路由 -- 组件 // test1 --- test1.vue // //方式一: // this.app = new Vue({ // data(){ // return { // //当前的路由 // current:'/' // } // } // }); } init(){ console.log('ppp'); //1. 监听hash的变化 this.initEvents(); //2. 处理路由routerMap this.createRouterMap(); //3 . 初始化组件 , router-link, router-view this.initComponent(); //4.路由守卫 } //1. 监听hash的变化 initEvents(){ window.addEventListener('hashchange',this.onHashChange.bind(this),false) window.addEventListener('load',this.onHashChange.bind(this),false) } //2. 处理路由routerMap createRouterMap(){ // this.routerMap = { // '/goods':'component', // '/goods1':'component1', // } this.$options.routes.forEach((item)=>{ this.routerMap[item.path] = item; }) } onHashChange(e){ console.log('我监听到路由变化了'); //获取hash let hash = location.hash.slice(1) || '/'; this.current = hash; //方式一 } //3 . 初始化组件 , router-link, router-view initComponent(){ Vue.component('router-link',{ // props:['to'], props:{ to:String }, render:function(h){ // h-- createElemnt // 3个参数 // dom节点名 // 属性 // 子元素 return h('a',{ attrs:{ href:'#'+this.to } },[this.$slots.default]) } }); Vue.component('router-view',{ render:h=>{ console.log('ppp'); // console.log(this.app.current); //方式一 //this vuerouter实例化构造函数的对象 let component = this.routerMap[this.current].component; return h(component); } }) }}export default MYRouter;
转载地址:https://codeboy.blog.csdn.net/article/details/107440503 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月16日 07时05分38秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
生产者消费者问题
2019-04-29
本机电脑连接虚拟机redis失败解决方法
2019-04-29
DM365 应用层gpio控制
2019-04-29
linux i2c子系统abc
2019-04-29
CSS3 帧动画(Sprite,直译叫雪碧图)
2019-04-29
Java 父线程与子线程相互通信的方法
2019-04-29
Redis 六种淘汰策略和三种删除策略
2019-04-29
Java LinkedHashMap
2019-04-29
JPA 多线程同时对一条数据进行Update的问题
2019-04-29
JPA 多线程对数据进行更新,Update和Insert同时存在的问题
2019-04-29
Java 高性能队列Disruptor
2019-04-29
SpringBoot 使用https
2019-04-29
Java 读写锁
2019-04-29
JVM Minor GC、Full GC和Major GC
2019-04-29
SpringBoot @Scheduled 执行两次的问题
2019-04-29
tomcat配置JVM
2019-04-29
Ubuntu软件安装&卸载
2019-04-29
面试笔试易错知识点Java篇八
2019-04-29
弹性事务框架ETF4J——面向Java微服务的交易最终一致性解决方案
2019-04-29
【Scala 教程】Scala 条件与循环语句
2019-04-29