1、vueRouter路由的两种模式

哈希模式:以#号分割,前端路由一般用hash模式比较多,可以兼容ie8以上,井号后面不发起请求
历史模式:putstate()和replaceState()方法入栈出栈,兼容ie10以上

2、安装vueRouter4

npm install vue-router@4

3、创建路由

创建路由js文件
1、在根目录下创建路由目录,并创建index.js,在里面编写业务逻辑
2、路由映射表,createWebHashHistory是创建历史模式的路由
3、引入页面组件
4、创建路由
5、导出路由

// 1、路由映射表,createWebHashHistory是创建历史模式的路由
import {createRouter,createWebHashHistory,createWebHistory} from 'vue-router'//2、引入页面组件
import Home from '../components/Home.vue'
import Mine from '../components/Mine.vue'//3、创建路由对象
const routes=[{path:'/home',component:Home},{path:'/mine',component:Mine}
]//4、创建路由
const router=createRouter({history:createWebHashHistory(),//设置路由方位方式routes
})//5、导出路由
export default router

在main.js中引入这个路由
1、从路由js文件中引入如有
2、使用app.use使用路由

import { createApp } from 'vue'
import App from './App.vue'//引入路由对象
import router from './Router/index.js'const app=createApp(App)
//使用路由
app.use(router)
app.mount('#app')

在具体的vue文件中使用路由
标签对建立导航
标签对,建立页面展示

<template>
<div><!-- 导航 --><div id="nav"><router-link to="/home">首页</router-link><router-link to="/mine">我的</router-link></div><!-- 路由出口 --><router-view></router-view>
</div></template><script >export  default {setup (props,context){return {}}}</script><style>
</style>

4、使用路由重定向默认根路径访问

使用redirect进行重定向

{path:'/',redirect:'/home'},
//3、创建路由对象
const routes=[
//路由重定向
{path:'/',redirect:'/home'},                          {path:'/home',component:Home},
{path:'/mine',component:Mine}
]

5、router-link常用属性active-class

active-class:添加一个激活的样式

<div id="nav"><router-link to="/home" active-class="current">首页</router-link><router-link to="/mine">我的</router-link></div>

此外,router-link有一个默认的选中的样式类
.router-link-active{
font-size:18px;
color:goldenrod;
font-weight:bolder
}

6、router-link常用属性replace

replace重新替换一个栈,而不会有缓存

<div id="nav"><router-link to="/home" replace>首页</router-link><router-link to="/mine" replace>我的</router-link></div>

7、js实现路由跳转

步骤
1、通常要绑定事件
2、引入vue-router
3、导入自定义的路由对象router
4、push到具体的url地址上去
5、也可以是用replace

 router.push('/home')
const home=()=>{// router.push('/home')router.replace("/home")}

8、动态路由$route.params

  • 如新闻详情页就是动态路由(路径)
  • 动态路由通过id来绑定动态路径,需要冒号:id
  • 访问动态路由的时候也需要带/id才能访问到如:http://localhost:3000/#/news/001
  • 父组件的动态id可以在子组件中通过$route.params来获取
<p>新闻的id{{$route.params}}</p>

子组件也可以通过导入useRoute来获取动态路由id,useRouter.params.id

<template><div id="news"><div>新闻详情</div><p>新闻的id{{$route.params.id}}</p></div>
</template><script>import {useRoute} from 'vue-router'export default {name: 'News',setup(){const router=useRoute()console.log(router)console.log(router.params.id)}}
</script>

案例

<template>
<div><!-- 导航 --><div id="nav"><router-link to="/home" replace>首页</router-link><router-link to="/mine" replace>我的</router-link><router-link :to="'/news/' + newsId " replace>新闻</router-link></div><!-- 路由出口 --><router-view></router-view></div></template><script >import {useRouter} from 'vue-router'import router from './router/index.js' //从router文件夹中取值。实际上就是导入的router/index.jsimport {ref} from 'vue'export  default {setup (props,context){const newsId=ref('fr001')return {newsId}}}</script>

9、路由懒加载

组件在路由访问的时候才加载,而不是一开始就加载,从而更加高效
路由index文件

//路由懒加载
const Home=()=>import('./../components/Home.vue')
const Mine=()=>import('./../components/Mine.vue')
const News=()=>import('./../components/News.vue')

案例

// 1、路由映射表,createWebHashHistory是创建历史模式的路由
import {createRouter,createWebHashHistory,createWebHistory} from 'vue-router'//2、引入页面组件
// import Home from './../components/Home.vue'
// import Mine from './../components/Mine.vue'
// import News from './../components/News.vue'//路由懒加载
const Home=()=>import('./../components/Home.vue')
const Mine=()=>import('./../components/Mine.vue')
const News=()=>import('./../components/News.vue')//3、创建路由对象
const routes=[{path:'/',redirect:'/home'},{path:'/home',component:Home},{path:'/mine',component:Mine},//动态路由,动态绑定id,要使用冒号:{path:'/news/:id',component:News}
]//4、创建路由
const router=createRouter({history:createWebHashHistory(),//设置路由方位方式routes
})//5、导出路由
export default router

10、路由嵌套

多级路由,如news/beijing/xxxx
步骤

  • 1、创建多个二级组件(vue文件)
  • 2、router的index.js编写二级路由的逻辑。使用children创建二级路由
children:[{path:'xx',component:xxx}
]
  • 3、在父级组件中使用这个二级组件
<div class="content-nav"><router-link to="/mine/msg">我的消息</router-link><br/><router-link to="/mine/order">我的订单</router-link></div><router-view class="content-list"></router-view></div><MineMsg></MineMsg>

案例:

  • 1、MineOrder.vue MinMsg.vue
<template><div id="mine_Order">我的订单</div>
</template><script>export default {name: 'MineOrder'}
</script>
<template><div id="mine_msg">我的消息</div>
</template><script>export default {name: 'MineMsg'}
</script>
  • 2、/router/index.js
// 1、路由映射表,createWebHashHistory是创建历史模式的路由
import {createRouter,createWebHashHistory,createWebHistory} from 'vue-router'//2、引入页面组件
import Home from './../components/Home.vue'
import Mine from './../components/Mine.vue'
import News from './../components/News.vue'
import MineMsg from './../components/MineMsg.vue'
import MineOrder from './../components/MineOrder.vue'//3、创建路由对象
const routes=[{path:'/',redirect:'/home'},{path:'/home',component:Home},//含有二级路由,不需要加斜杠{path:'/mine',component:Mine,children:[{path:'/mine',redirect:'/mine/msg'},{path:'msg',component:MineMsg},{path:'order',component:MineOrder},]},//动态路由,动态绑定id,要使用冒号:{path:'/news/:id',component:News}
]//4、创建路由
const router=createRouter({history:createWebHashHistory(),//设置路由方位方式routes
})//5、导出路由
export default router
  • 3、Mine.vue父组件
<template><div id="mine"><h2>我的</h2><div class="content"><div class="content-nav"><router-link to="/mine/msg">我的消息</router-link><br/><router-link to="/mine/order">我的订单</router-link></div><router-view class="content-list"></router-view></div><MineMsg></MineMsg></div>
</template><script>import MineMsg from './MineMsg.vue'export default {name: 'Mine',components: { MineMsg }}
</script>

11、路由参数传递-params

动态路由路径为:/router/:params
传递方式为在path后面跟上传递的参数
传递后形成的路径:/router/lk001

12、路由参数传递-query

路由格式:/router
传递方式,在对象中使用query的key作为传递方式
传递后形成的路径/router?id=lk001
以上两个知识点案例
app.vue

<template>
<div><!-- 导航 --><div id="nav"><router-link to="/home" replace>首页</router-link><router-link to="/mine" replace>我的</router-link><router-link :to="'/news/' + newsId " replace>新闻</router-link><router-link to="/circle" >圈子parmas</router-link><!-- 直接传参 --><router-link :to="{path:'circle',query:{name:'sina',site:'www.sina.com.cn'}}" >圈子query</router-link><!-- 通过query绑定对象来传递 --></div><!-- 路由出口 --><router-view></router-view></div></template><script >import {useRouter} from 'vue-router'import router from './router/index.js' //从router文件夹中取值。实际上就是导入的router/index.jsimport {ref} from 'vue'export  default {setup (props,context){const newsId=ref('fr001')return {newsId}}}</script><style>.router-link-active{font-size:18px;color:goldenrod;font-weight:bolder}
</style>

index.js引入模块及配置路由

// 1、路由映射表,createWebHashHistory是创建历史模式的路由
import {createRouter,createWebHashHistory,createWebHistory} from 'vue-router'//2、引入页面组件
import Home from './../components/Home.vue'
import Mine from './../components/Mine.vue'
import News from './../components/News.vue'
import MineMsg from './../components/MineMsg.vue'
import MineOrder from './../components/MineOrder.vue'
import Circle from './../components/Circle.vue'
//3、创建路由对象
const routes=[{path:'/',redirect:'/home'},{path:'/home',component:Home},//含有二级路由,不需要加斜杠{path:'/mine',component:Mine,children:[{path:'/mine',redirect:'/mine/msg'},{path:'msg',component:MineMsg},{path:'order',component:MineOrder},]},//动态路由,动态绑定id,要使用冒号:{path:'/news/:id',component:News},{path:'/circle',component:Circle},
]//4、创建路由
const router=createRouter({history:createWebHashHistory(),//设置路由方位方式routes
})//5、导出路由
export default router

circle.vue模板页

<template><div id="circle">圈子<p>{{$route.query.site}}</p><p>{{$route.query.name}}</p></div>
</template><script>import {useRoute} from 'vue-router'export default {name: 'Circle',setup(){let route=useRoute()//获得当前路由console.log(route.query)}}
</script><style scoped>
#circle{width: 400px;height: 600px;background-color: cadetblue;
}
</style>

两个不同的地址栏表现形式

http://localhost:3000/#/circle
另一个
http://localhost:3000/#/circle?name=sina&site=www.sina.com.cn

13、通过事件来跳转路由

app.vue

<template>
<div><!-- 导航 --><div id="nav"><router-link to="/home" replace>首页</router-link><router-link to="/mine" replace>我的</router-link>
<!--      <router-link :to="'/news/' + newsId " replace>新闻</router-link>-->
<!--      <router-link to="/circle" >圈子parmas</router-link>&lt;!&ndash; 导航 &ndash;&gt;-->
<!--      <router-link :to="{path:'circle',query:{name:'sina',site:'www.sina.com.cn'}}" >圈子query</router-link>&lt;!&ndash; 通过绑定对象来传递 &ndash;&gt;--><button @click="news()">新闻</button><button @click="circle()">圈子</button></div><!-- 路由出口 --><router-view></router-view></div></template><script >import {useRouter} from 'vue-router'// import router from './router/index.js' //从router文件夹中取值。实际上就是导入的router/index.jsimport {ref} from 'vue'export  default {setup (props,context){const newsId=ref('fr001')const router=useRouter()const news=()=>{router.push('/news/' + newsId.value )}const circle=()=>{router.push({path:'/circle',query:{name:'sina',site:'www.sina.com.cn',age:10}})}return {newsId,news,circle}}}</script><style>.router-link-active{font-size:18px;color:goldenrod;font-weight:bolder}
</style>

Router/index.js

// 1、路由映射表,createWebHashHistory是创建历史模式的路由
import {createRouter,createWebHashHistory,createWebHistory} from 'vue-router'//2、引入页面组件
import Home from './../components/Home.vue'
import Mine from './../components/Mine.vue'
import News from './../components/News.vue'
import MineMsg from './../components/MineMsg.vue'
import MineOrder from './../components/MineOrder.vue'
import Circle from './../components/Circle.vue'
//3、创建路由对象
const routes=[{path:'/',redirect:'/home'},{path:'/home',component:Home},//含有二级路由,不需要加斜杠{path:'/mine',component:Mine,children:[{path:'/mine',redirect:'/mine/msg'},{path:'msg',component:MineMsg},{path:'order',component:MineOrder},]},//动态路由,动态绑定id,要使用冒号:{path:'/news/:id',component:News},{path:'/circle',component:Circle},
]//4、创建路由
const router=createRouter({history:createWebHashHistory(),//设置路由方位方式routes
})//5、导出路由
export default router

circle.vue

<template><div id="circle">圈子<p>{{$route.query}}</p><p>{{$route.query.site}}</p><p>{{$route.query.name}}</p><p>{{$route.query.age}}</p></div>
</template><script>import {useRoute} from 'vue-router'export default {name: 'Circle',setup(){let route=useRoute()//获得当前路由console.log('dfdfasdfasdfsfsa')console.log(route.query)}}
</script><style scoped>
#circle{width: 400px;height: 600px;background-color: green;
}
</style>

14、VueRouter-router和Route的区别

VueRouter-router是全局的,可全局使用
Route是局部使用
好像这么说也不是太准确

Vue3(撩课学院)笔记07-vueRouter路由,创建路由,路由重定向,路由跳转,动态路由,路由懒加载,路由嵌套,路由参数穿传递的两种形式,通过事件来跳转路由相关推荐

  1. Vue 路由懒加载和动态加载

    什么是路由懒加载? 不同组件有不同的 js 文件,当访问相应组件的时候才会加载其相应的js文件,而不是在首页统一加载,这样就优化了首页渲染的时间,提高页面首次渲染时间: 路由懒加载的原理? 底层是一个 ...

  2. vue 之 路由懒加载

    vue打包后的js文件越来越大,这会是影响加载时间的重要因数.当构建的项目比较大的时候,懒加载可以分割代码块,提高页面的初始加载效率.下面是几种常见vue路由懒加载的方法. 方法一 resolve 这 ...

  3. VUE动态路由组件懒加载解决方案

    项目场景: 将项目静态路由替换成动态路由,通过后台数据拿到路由配置信息动态加载 问题描述: 小编将路由信息与系统菜单信息存在一起,当登录后拿到菜单信息后同时处理路由数据,过程肯定不是那么一帆风顺的,下 ...

  4. Vue(小码哥王洪元)笔记06路由,url的hash,history,router-linke,路由跳转,动态路由,懒加载,路由嵌套,router参数传递,导航守卫

    1.什么是路由 路由器提供了两种机制:路由和传送 路由:数据报从来源到目的地的路径 传输:将输入端的数据转移到合适的输出端 路由有一个非常重要的概念教路由表 路由表本质上就是一个映射表,决定了数据包的 ...

  5. vue-router路由懒加载

    路由懒加载指的是打包部署时将资源按照对应的页面进行划分,需要的时候加载对应的页面资源,而不是把所有的页面资源打包部署到一块.避免不必要资源加载.(参考vue项目实现路由按需加载(路由懒加载)的3种方式 ...

  6. “约见”面试官系列之常见面试题之第一百零三篇之vue-router实现路由懒加载(建议收藏)

    vue异步组件 es提案的import() webpack的require,ensure() vue异步组件技术 ==== 异步加载 vue-router配置路由 , 使用vue的异步组件技术 , 可 ...

  7. Vue-Router + Webpack 路由懒加载实现

    一.前言 https://segmentfault.com/a/1190000015904599 当打包构建应用时,Javascript 包会变得非常大,影响页面加载.如果我们能把不同路由对应的组件分 ...

  8. vue-router路由懒加载(解决vue项目首次加载慢)

    懒加载:----------------------------------------------------? 也叫延迟加载,即在需要的时候进行加载,随用随载. 为什么需要懒加载? 像vue这种单 ...

  9. vue-router 路由的懒加载原理及方式

    当打包构建应用时,JavaScript 包会变得非常大,影响页面加载.如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了. 结合 Vue 的异步组 ...

最新文章

  1. 【Windows10nbsp;IoT开发系列】“Hello,World!”指导
  2. SharePoint 2007部署过程详细图解之一:准备
  3. Can't connect to X11 window server using ':1.0' as the value of the DISPLAY variable.
  4. springmvc学习笔记--Interceptor机制和实践
  5. TCP/IP总结(4)TCP之数据传输过程
  6. 最优化(part1)--近似点梯度法
  7. TCP/IP Model: Layers Protocol | What is TCP IP Stack?
  8. 名片夹android布局代码,Android自定义布局实现仿qq侧滑部分代码
  9. 把算术表达式 表示为一个c语言表达式,正确的写法是____,C与C++程序设计-中国大学mooc-题库零氪...
  10. JavaScript 丨 你不知道的arguments
  11. 复杂网络分析 05 无标度网络
  12. Android 桌面小部件的背景透明度及颜色的动态实现
  13. 关于华为鸿蒙创新的作文,关于创新的高中作文4篇
  14. 21届校招中获得12家公司offer的一些经验(包括6家银行信息科技岗offer)
  15. 用Python的matplotlib绘制残差分析散点误差棒图
  16. 国科大五位本科生带“芯”毕业!平均年龄23.1岁,四个月主导完成64位RISC-V处理器SoC芯片设计并实现流片
  17. 使用Python编写面向安卓模拟器的明日方舟挂机脚本
  18. 黑马毕向东Java课程笔记(day14-1——14-11):集合类(集合框架)——集合类分类与特点+List集合接口及其子类
  19. thinkpad x61-lg2装XP
  20. Retrofit2---网络交互的艺术

热门文章

  1. linux实用技巧:ubuntu16.04安装BeyondCompare文件/文件夹对比工具
  2. [Python] PyQt5+Pycharm 实现省、市、区/县三级联动效果
  3. 【狂神说Java】Redis最新超详细版教程通俗易懂1 2020-04-01:NoSQL分类,Redis Windows和Linux安装,性能测试,String,List,Set,Hash,Zset
  4. sae php mysql数据库,php连接mysql数据库(新浪云SAE)
  5. STM32CubeIDE(stm32f767)添加DSP库
  6. VS+openCV 用直方图统计像素(上)计算图像直方图、利用查找表修改图像外观
  7. Centos无网环境下安装mysql步骤
  8. 计算机类 ei,sciei收录的计算机类期刊(37页)-原创力文档
  9. STM32 移植FreeModbus详细过程
  10. 算法刷题系列(四)蓝桥杯python算法训练3(下)