首页 > 其他分享 >vue-router

vue-router

时间:2023-04-09 20:11:25浏览次数:44  
标签:vue const next Vue ._ router matched

###################

npm install vue-router

 

 

 

 

 

 

 Vue.use()方法的源代码如下:

function install(Vue) {
  // 避免重复安装插件
  if (install.installed && _Vue === Vue) return
  install.installed = true
  _Vue = Vue
  // 执行插件的安装方法
  const isDef = val => val !== undefined

  const registerInstance = (vm, callVal) => {
    let i = vm.$options._parentVnode
    if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
      i(vm, callVal)
    }
  }
  _Vue.mixin({
    beforeCreate() {
      // 如果有router选项,则把router挂在到vue实例上
      if (isDef(this.$options.router)) {
        this._routerRoot = this
        this._router = this.$options.router
        this._router.init(this)
        Vue.util.defineReactive(this, '_route', this._router.history.current)
      } else {
        this._routerRoot = (this.$parent && this.$parent._routerRoot) || this
      }
      registerInstance(this, this)
    },
    destroyed() {
      registerInstance(this)
    },
  })
  // 注册router-link和router-view全局组件
  _Vue.component('RouterView', View)
  _Vue.component('RouterLink', Link)
  
  // 在Vue原型上定义$router和$route属性
  Object.defineProperty(Vue.prototype, '$router', {
    get() { return this._routerRoot._router }
  })
  Object.defineProperty(Vue.prototype, '$route', {
    get() { return this._routerRoot._route }
  })

  // 注册钩子函数,用于处理VueRouter的生命周期
  Vue.mixin({
    beforeRouteEnter(to, from, next) {
      next(vm => {
        if (!vm._isMounted) {
          // 如果Vue组件没有挂载,则在挂载时再执行beforeRouteEnter函数
          vm.$nextTick(() => {
            next(vm)
          })
        }
      })
    },
    beforeRouteUpdate(to, from, next) {
      const { matched } = this.$route
      const prevMatched = from.matched
      let diffed = false
      const activated = matched.filter((c, i) => {
        return diffed || (diffed = (prevMatched[i] !== c))
      })
      if (!activated.length) {
        this.$nextTick(() => {
          next()
        })
      } else {
        const queue = activated.map(c => {
          return new Promise((resolve, reject) => {
            if (c.beforeRouteEnter) {
              c.beforeRouteEnter(to, from, () => {
                resolve()
              })
            } else {
              resolve()
            }
          })
        })
        Promise.all(queue).then(() => {
          next()
        }).catch(() => {
          next(false)
        })
      }
    },
    beforeRouteLeave(to, from, next) {
      const matched = this.$route.matched
      const component = matched[matched.length - 1]
      if (!component || !component.beforeRouteLeave) {
        next()
      } else {
        component.beforeRouteLeave(to, from, () => {
          next()
        })
      }
    }
  })
}

export default {
  install,
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

##########################

标签:vue,const,next,Vue,._,router,matched
From: https://www.cnblogs.com/igoodful/p/17300945.html

相关文章

  • Vue开发规范
    ###############################单文件组件内容:<!--componentA.vue--><script>/*...*/</script><template>...</template><style>/*...*/</style>   组件相关:尽可能的减少watcher的数量尽量减少组件嵌套,递归渲染影响性能大量数据渲染导致卡顿,可以先渲染用户可见......
  • Vue——patch.ts【十四】
    前言前面我们简单的了解了vue初始化时的一些大概的流程,这里我们扩展下Vue的patch。内容这一块主要围绕vue中的__patch__进行剖析。__patch__Vue.prototype.__patch__的方法位于scr/platforms/web/runtime/index.ts中;//installplatformpatchfunction//判断是......
  • Vue Router 学习整理
    VueRouter是Vue.js的官方路由。它与Vue.js核心深度集成,让用Vue.js构建单页应用变得轻而易举。功能包括:嵌套路由映射动态路由选择模块化、基于组件的路由配置路由参数、查询、通配符展示由Vue.js的过度系统提供的过渡效果细致的导航控制自动激活CSS类的链接HTML5histo......
  • VUE-Antd开发,validate规则校验第一次为空,后面无论怎么输入都显示不可为空
    问题描述场景是修改弹窗,给数据赋值,其中数据有String和Array第一次操作多选框(数据类型是Array)输入为空并在输入框外点击,显示不可为空。之后无论怎么输入都显示不可为空。(当你控制台输出校验value时,会发现里面的值永远是第一次操作的数据,而非为空)可能原因1️⃣rules定义出错......
  • ant-design-vue日历面板 a-calendar 属性自定义设置
    通过自定义属性设置,实现一个周末与工作日的不同颜色设置效果图: 使用的属性:自定义头部设置headerRender自定义日期显示dateFullCellRender代码:<template><divclass="box"><h3>1.自定义头部;2.自定义日期显示,工作日显示,周末显示</h3><a-c......
  • 将Vue项目部署到Tomcat服务器上(简单、粗暴)
    1.将项目打包Vue中自带webpack,可以通过一行命令将项目打包#执行该命令打包npmrunbuild2.上传相应文件到服务器上打包完成后,项目中会多出一个dist文件夹,这个文件夹中就包含html、css、js等文件直接将dist文件夹上传到Tomcat的ROOT目录下,默认情况下,访问服务器网址......
  • Vue常用指令
    常用指令指令:HTML标签上带有V-前缀的特殊属性,不同指令具有不同含义。常用指令指令作用v-bind为HTML标签绑定属性值,如设置href,css样式等v-model在表单元素上创建双向数据绑定v-on为HTML标签绑定事件v-if条件性的渲染某元素,判定为true时渲染,否则不......
  • html页面里面的button标签使用@click属性时,无法定位到Vue的method里面
    问题解决就很离谱,都是按照网上的教程来的,一直无法定位,之后跟着加上了div标签,加上了id属性,方法还是灰白色,调用不了;后来直接将el属性名称更改掉了,没想到这样就成功识别到了,反正就是逻辑没有出问题,最后也达到期望值了。......
  • vscode 开发 vue3项目 , src 别名 为 @ ,报错
    https://geekdaxue.co/read/me-note@vue/mydm8l需要设置basicURL然后就生效了{"compilerOptions":{//设置解析非相对模块名称的基本目录"baseUrl":".",//设置模块名到基于baseUrl的路径映射,可以设置路径别名的语法提示"paths":{"@/*......
  • vue2源码-一、rollup环境配置
    npminit-y创建初始化package.json并修改script为"dev":"rollup-cw"安装rollup及其插件:npminstallrolluprollup-plugin-babel@babel/core@babel/preset-env--save-dev创建rollup.config.js对rollup进行配置:配置如下:importbabelfrom'rollup-plugin-bab......