首页 > 其他分享 >vue-router

vue-router

时间:2023-04-17 15:02:48浏览次数:33  
标签:vue const next Vue ._ router matched

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

npm install vue-router

 

vue-router_类名

 

 

 

 

vue-router_安装方法_02

 

 

vue-router_安装方法_03

 

 

 

 

 

 

 

 

vue-router_安装方法_04

 

 

vue-router_安装方法_05

 

 

 

vue-router_安装方法_06

 

 

vue-router_类名_07

 

 

vue-router_Vue_08

 

 

 

vue-router_类名_09

 

 

vue-router_安装方法_10

 

 

 

vue-router_Vue_11

 

 

<router-link to="/user" tag="button" active-class="active" exact>Go to User Page</router-link>

这个例子中,当用户点击按钮时,会跳转到 /user 路由,并且按钮会添加 active 类名。另外,由于开启了精确匹配模式,只有当路径与目标路由完全匹配时才会添加 active 类名。

 

vue-router_安装方法_12

 

 

 

 

 

vue-router_类名_13

 

 

 

vue-router_安装方法_14

 

 

vue-router_安装方法_15

 

 

 

 

 

 

 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://blog.51cto.com/u_13912516/6195197

相关文章

  • vue之插槽
    #######################                    #########################......
  • Vue开发规范
    ###############################有赞:https://github.com/youzan/vanthttps://github.com/iview/iviewhttps://github.com/ElemeFE/elementhttps://github.com/JosephusPaye/Keen-UIhttps://github.com/ElemeFE/mint-uihttps://github.com/museui/muse-uihttps://github.com/yo......
  • 【vue3-element-admin】Husky + Lint-staged + Commitlint + Commitizen + cz-git 配
    前言本文介绍vue3-element-admin如何通过Husky+Lint-staged+Commitlint+Commitizen+cz-git来配置Git提交代码规范。核心内容是配置Husky的pre-commit和commit-msg两个钩子:pre-commit:Husky+Lint-staged整合实现Git提交前代码规范检测/格式化(前提......
  • Vue3中 如何使用ref标签,对组件进行操作
    在Vue2中一般用this.$ref.xxxx进行获取组件对象Vue3中就不使用这个方法了例如:<el-uploadclass="upload-demo"action="":http-request="handleUpload":on-change="handleChange":before-upload="handl......
  • vue3中使用ref语法糖
    自从引入组合式API的概念以来,一个主要的未解决的问题就是ref和响应式对象到底用哪个。响应式对象存在解构丢失响应性的问题,而ref需要到处使用.value则感觉很繁琐,并且在没有类型系统的帮助时很容易漏掉.value写法优化以上是官方原话。大概就是新的语法糖可以让我们......
  • 安装vue-lic
    vue-cli是Vue.js开发的标准工具。它简化了程序员基于webppack创建工程化的Vue项目的过程。引用自vue-cli官网上的一句话:程序员可以专注在撰写应用上,而不必花好几天去纠结\webpack配置的问题。安装和使用(1)确保电脑已经安装了node.js,如果不确定自己是否安装了node,可以在cmd命令......
  • Vue3 vue-cli创建工程的工程结构分析
    视频不能用vue2的写法了componentsHelloWorld.vue<template><divclass="hello"><h1>{{msg}}</h1><p>Foraguideandrecipesonhowtoconfigure/customizethisproject,<br>checkoutthe......
  • vue中开启https
    vue2.0中项目工程根目录下,找到文件 vue.config.js。设置 module.exports.devServer.https:true项目工程根目录下,找到文件vue.config.js。设置module.exports.devServer.https:truemodule.exports={productionSourceMap:false,configureWebpack:{devt......
  • vue table 里面 slot 的模板复用 slot-scope template v-for
    vuetable里面slot的模板复用slot-scopetemplatev-for需求经常在table里面要有自定义列,但是会有相同的自定义列,这个时候又不想写很多一样的template,就可以用这种方式代码<template:slot="slotName"v-for="slotNamein['slotName1','slotName2','slot......
  • Vue中单选框或复选框中的label内容过长,超出范围
     解决办法:1、直接把字体调小2、当需要的字体大小还是超出了范围时在css中写(如果是复选框;单选框的class:el-radio__label).el-checkbox__label{text-overflow:ellipsis;white-space:normal;line-height:18px;......