vue 强制刷新组件_深入理解vue-router原理

说到vue-router就表明他只适合于vue和vue是强绑定的关系;不适合其他框架;

现在我们模仿实现一个VueRouter;

1.要使页面刷新;借助vue本身的响应式原理;

import Home from "./views/Home";import About from "./views/About";import Vue from "vue";   class VueRouter{    constructor(options){        this.$options = options;        this.routeMap = {}        // 路由响应式 其实很重要的一部分是借助了 vue 的响应式原理 ; 强绑定 ; 必须用在vue上        this.app = new Vue({            data:{                current:"/"            }        })    }}VueRouter.install=function(Vue){    //mixin 混入    Vue.mixin({        beforeCreate() {            // this 是vue 实咧            if(this.$options.router){  // 只在跟组件执行一次                Vue.prototype.$router = this.$options.router;                this.$options.router.init()            }        },    }) }Vue.use(VueRouter)export default new VueRouter({    routes: [{ path: "/", component: Home }, { path: "/about", component: About }]})

2.执行上面的init函数;初始化

//这里主要三件事;监听url变化;解析路由配置;实现两个组件init(){        this.bindEvents() ; //监听url变化        this.createRouteMap(this.$options) ; //解析路由配置        this.initComponent() ; //实现两个组件 router-view  router-link    }

3.监听url变化;更新数据

  bindEvents(){        window.addEventListener('load',this.onHashChange.bind(this))        window.addEventListener('hashchange',this.onHashChange.bind(this))    }    onHashChange(){        this.app.current = window.location.hash.slice(1) || '/';  // #/about  ==> /about    }

4.解析路由配置;

//这个好理解;就是把传进来的路由配置;解析成path-->component的key:value形式createRouteMap(options){        options.routes.forEach(item=>{            this.routeMap[item.path] = item.component        })  }

5.实现router-link 和 router-view两个组件

 initComponent(){        // router-view router-link        Vue.component('router-link',{              props:{to:String},            render(h) {                // h => createElement  h(tag,data,children)                return h('a',{attrs:{href:'#'+this.to}},[                    this.$slots.default                ])            },        });        Vue.component('router-view',{     //这里没有解决路由嵌套的问题   // 全局的组件 任意data 的都会出发全局组件的跟新            render:h=>{                console.log(this.routeMap[this.app.current])                const comp = this.routeMap[this.app.current]                return h(comp)            },        })    }

vue-router的hash模式主要就是监听了hashchange方法;改变当前的路径,再利用vue自身的响应式原理;来刷新视图;

vue-history模式;要服务器配合;访问进来是指向同一个html;代码上处理原理我想和hash应该类似;大家可以自己研究一下;

d82db9b304c2e99e36650843bff57d55.png

人到了一定的年龄,一定要懂得,把心情和脾气调成静音模式,然后不动声色打理自己,过好余生每一天。一路风雨兼程,只为遇见更好的自己,真正能够带你走出黑暗,能够给你带来改变的,就是你自己的内心自信,光明,有拯救自己的责任感动力

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WARN "css.modules" option in vue.config.js is deprecated now, please use "css.requireModuleExtension" instead. INFO Starting development server... 98% after emitting CopyPlugin WARNING Compiled with 17 warnings 09:43:57 warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'computed' was not found in 'vue' warning in ./src/router/index.js "export 'default' (imported as 'VueRouter') was not found in 'vue-router' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'defineComponent' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'getCurrentInstance' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'h' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'inject' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'nextTick' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'onActivated' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'onDeactivated' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'onUnmounted' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'provide' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'reactive' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'ref' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'shallowRef' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'unref' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'watch' was not found in 'vue' warning in ./node_modules/vue-router/dist/vue-router.mjs "export 'watchEffect' was not found in 'vue'这个报错因为什么
06-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值