位置: IT常识 - 正文

Vue3的vue-router超详细使用(vue–router)

编辑:rootadmin
Vue3的vue-router超详细使用 从零开始搭建Vue3环境(vite+ts+vue-router),手拉手做一个router项目搭建vue3环境vue-router入门(宝宝模式)vue-router基础(青年模式)一。动态路由匹配1.带参数的动态路由匹配2.捕获所有路由或404 Not Found路由二。嵌套路由三。编程式导航1.router.push()方法的使用2.router.replace()方法的使用3.router.go()方法的使用搭建vue3环境

推荐整理分享Vue3的vue-router超详细使用(vue–router),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue3 vue-router,vue3.0 router,vue $route,vue–router,vue3 vue-router,vue3 vue-router,vue3 router-view,vue3.0 router,内容如对您有帮助,希望把文章链接给更多的朋友!

我们使用vite来搭建vue3环境(没有安装vite需要去安装vite)

npm create vite routerStudy

在命令行选择

cd routerStudynpm inpm run dev

环境搭建成功!!

vue-router入门(宝宝模式)下载vue-routernpm i vue-router@4新建以下文件 src/components/File1.vue:<template> <div> 这是文件一 </div></template><script setup lang="ts"></script><style scoped></style>

src/components/File2.vue:

<template> <div> 这是文件二 </div></template><script setup lang="ts"></script><style scoped></style>

在src下新建router文件夹 在router文件夹下新建router.ts:

import { createRouter,createWebHistory,createWebHashHistory } from 'vue-router'import File1 from '../components/File1.vue'import File2 from '../components/File2.vue'const routes = [ { path: '/', component:File1 }, { path: '/file2', component:File2 }]const router = createRouter({ // history: createWebHistory(), history:createWebHashHistory(), routes,})export default router;修改src/main.tsimport { createApp } from 'vue'import './style.css'import App from './App.vue'import router from './router/router'createApp(App).use(router).mount('#app')修改src/components/HelloWorld.vue:<script setup lang="ts"></script><template> <router-view/> <button><router-link to="/">去文件一</router-link></button> <button><router-link to="/file2">去文件二</router-link></button> </template><style scoped></style>点击按钮能够切换成功则使用成功vue-router基础(青年模式)一。动态路由匹配1.带参数的动态路由匹配

当我们需要对每个用户加载同一个组件,但用户id不同。我们就需要在vue-router种使用一个动态字段来实现,再通过$routr.params来获取值:

我们用具体实例来实现一下:

(1)修改src/router/router.ts:

import { createRouter,createWebHistory,createWebHashHistory } from 'vue-router'import File1 from '../components/File1.vue'import File2 from '../components/File2.vue'const routes = [ { path: '/', component:File1 }, { path: '/file2/:username/abc/:userid', //注意看这个 component:File2 }]const router = createRouter({ history: createWebHistory(), // history:createWebHashHistory(), routes,})export default router;

(2)修改组件HelloWorld.vue: (3) 修改组件File2.vue:

<template> <div> 这是文件二 </div></template><script setup lang="ts">import {getCurrentInstance,onMounted } from 'vue'const instance = getCurrentInstance() if (instance != null) { const _this = instance.appContext.config.globalProperties //vue3获取当前thisonMounted(() => { console.log(_this.$route.params) })}</script><style scoped></style>

(4)点击去文件二按钮 (5)查看控制台

2.捕获所有路由或404 Not Found路由Vue3的vue-router超详细使用(vue–router)

当用户在导航栏乱输一通后,路由表中没有对应的路由,这时候,就需要将用户转去404页面。那么 我们该如何处理呢?

(1)修改router/router.ts:

import { createRouter,createWebHistory,createWebHashHistory } from 'vue-router'import File1 from '../components/File1.vue'import File2 from '../components/File2.vue'import NotFound from '../components/NotFound.vue'import UserGeneric from '../components/UserGeneric.vue'const routes = [ { path: '/', component:File1 }, { path: '/file2/:username/abc/:userid', component:File2 }, // 将匹配所有内容并将其放在 `$route.params.pathMatch` 下 { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound }, // 将匹配以 `/user-` 开头的所有内容,并将其放在 `$route.params.afterUser` 下 { path: '/user-:afterUser(.*)', component: UserGeneric },]const router = createRouter({ history: createWebHistory(), // history:createWebHashHistory(), routes,})export default router;

(2)新建组件NotFound.vue:

<template> <div> 糟糕!页面没有找到。。。呜呜呜 </div></template><script setup lang="ts">import {getCurrentInstance,onMounted } from 'vue'const instance = getCurrentInstance() if (instance != null) { const _this = instance.appContext.config.globalProperties //vue3获取当前thisonMounted(() => { console.log(_this.$route.params) })}</script><style scoped></style>

(3)修改HelloWorld.vue (4)点击去404页面按钮(或者在地址栏乱写一通) (5)出现404页面,说明运行成功!!!

二。嵌套路由

路由是可以嵌套的。例如: 嵌套的理解挺简单的,我就不多叭叭了,直接上代码,看完就懂了。 (1)新建组件Children1.vue:

<template> <div> 我是孩子1 </div></template><script setup lang="ts"></script><style scoped></style>

(2)新建组件Children2.vue:

<template> <div> 我是孩子2 </div></template><script setup lang="ts"></script><style scoped></style>

(3)修改router/router.ts:

import { createRouter,createWebHistory,createWebHashHistory } from 'vue-router'import File1 from '../components/File1.vue'import File2 from '../components/File2.vue'import NotFound from '../components/NotFound.vue'import UserGeneric from '../components/UserGeneric.vue'import Children1 from '../components/Children1.vue'import Children2 from '../components/Children2.vue'const routes = [ { path: '/', component: File1, }, { path: '/file2', component: File2, children: [ //使用嵌套路由 { path: 'children1', component:Children1 }, { path: 'children2', component:Children2 }, ] }, { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound }, { path: '/user-:afterUser(.*)', component: UserGeneric },]const router = createRouter({ history: createWebHistory(), // history:createWebHashHistory(), routes,})export default router;

(4)修改组件HelloWorld.vue: (5)修改组件File2.vue:

<template> <div> 这是文件二 <div> 我是文件二里的内容 <router-view/> <button><router-link to="/file2/children1">找孩子1</router-link></button> <button><router-link to="/file2/children2">找孩子2</router-link></button> </div> </div></template><script setup lang="ts"></script><style scoped></style>

(6)先点去文件二,再点击找孩子1按钮,出现即成功!!

三。编程式导航

除了使用/< router-link/> 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现。

1.router.push()方法的使用

(1)修改组件NotFound.vue:

<template> <div> 糟糕!页面没有找到。。。呜呜呜 </div></template><script setup lang="ts">import {getCurrentInstance,onMounted } from 'vue'const instance = getCurrentInstance() if (instance != null) { const _this = instance.appContext.config.globalProperties //vue3获取当前this // 1.字符串路径 _this.$router.push('/file2/children2') // 2.带有路径的对象 // _this.$router.push({path:'/file2/children2'}) // 3.命名的路由,并加上参数,让路由建立 url // _this.$router.push({name:'file2',params:{username:'children2'}}) // 4.带查询参数,结果是 /register?plan=private // _this.$router.push({ path: '/file2/children2', query: {userid:'123'} }) onMounted(() => { console.log(_this.$route.params) })}</script><style scoped></style>

(2)再点“去404页面”,发现没有去404页面了,说明编程式导航成功!!

2.router.replace()方法的使用

它的作用类似于 router.push,唯一不同的是,它在导航时不会向 history 添加新记录,正如它的名字所暗示的那样——它取代了当前的条目。

修改组件NotFound.vue:

<template> <div> 糟糕!页面没有找到。。。呜呜呜 </div></template><script setup lang="ts">import {getCurrentInstance,onMounted } from 'vue'const instance = getCurrentInstance() if (instance != null) { const _this = instance.appContext.config.globalProperties //vue3获取当前this // 一。router.push的使用: // 1.字符串路径 // _this.$router.push('/file2/children2') // 2.带有路径的对象 // _this.$router.push({path:'/file2/children2'}) // 3.命名的路由,并加上参数,让路由建立 url // _this.$router.push({name:'file2',params:{username:'children2'}}) // 4.带查询参数,结果是 /register?plan=private // _this.$router.push({ path: '/file2/children2', query: {userid:'123'} }) // 二。router.replace的使用: _this.$router.replace('/file2/children1') onMounted(() => { console.log(_this.$route.params) })}</script><style scoped></style>3.router.go()方法的使用

修改组件NotFound.vue:

<template> <div> 糟糕!页面没有找到。。。呜呜呜 </div></template><script setup lang="ts">import {getCurrentInstance,onMounted } from 'vue'const instance = getCurrentInstance() if (instance != null) { const _this = instance.appContext.config.globalProperties //vue3获取当前this // 一。router.push的使用: // 1.字符串路径 // _this.$router.push('/file2/children2') // 2.带有路径的对象 // _this.$router.push({path:'/file2/children2'}) // 3.命名的路由,并加上参数,让路由建立 url // _this.$router.push({name:'file2',params:{username:'children2'}}) // 4.带查询参数,结果是 /register?plan=private // _this.$router.push({ path: '/file2/children2', query: {userid:'123'} }) // 二。router.replace的使用: // _this.$router.replace('/file2/children1') // 三。router.go的使用: _this.$router.go(-1) //相当于点击回退一次 onMounted(() => { console.log(_this.$route.params) })}</script><style scoped></style>
本文链接地址:https://www.jiuchutong.com/zhishi/294518.html 转载请保留说明!

上一篇:SQL代码编码原则和规范(sql代码大全)

下一篇:React框架第七课 语法基础课《第一课React你好世界》(react框架结构)

  • oracle触发器写法(oracle语句触发器)

    oracle触发器写法(oracle语句触发器)

  • 腾讯地图怎么修改店铺名称(腾讯地图怎么修改家的位置)

    腾讯地图怎么修改店铺名称(腾讯地图怎么修改家的位置)

  • 微信申请的绿码在哪里(微信申请的绿码在哪里找)

    微信申请的绿码在哪里(微信申请的绿码在哪里找)

  • 剪映如何剪视频(剪映如何剪视频音乐)

    剪映如何剪视频(剪映如何剪视频音乐)

  • 苹果镜像找不到设备(苹果镜像找不到MacBook)

    苹果镜像找不到设备(苹果镜像找不到MacBook)

  • ctrl键加什么是截图

    ctrl键加什么是截图

  • 网易云访客记录怎么查(网易云访客记录软件)

    网易云访客记录怎么查(网易云访客记录软件)

  • pdf打印和word打印的区别(pdf打印和word打印出来格式一样吗)

    pdf打印和word打印的区别(pdf打印和word打印出来格式一样吗)

  • 淘宝店铺评分灰色和红色什么区别(淘宝店铺评分灰分怎么看)

    淘宝店铺评分灰色和红色什么区别(淘宝店铺评分灰分怎么看)

  • 抖音音浪比例(抖音音浪是怎么算的抖音收多少钱比例)

    抖音音浪比例(抖音音浪是怎么算的抖音收多少钱比例)

  • 苹果开原彩增加耗电不(iphone原彩模式开还是不开)

    苹果开原彩增加耗电不(iphone原彩模式开还是不开)

  • 苹果杜比音效怎么设置(苹果杜比音效是什么意思啊)

    苹果杜比音效怎么设置(苹果杜比音效是什么意思啊)

  • iphone8plus蜂窝网速慢(iphone8plus蜂窝网络空白怎么办)

    iphone8plus蜂窝网速慢(iphone8plus蜂窝网络空白怎么办)

  • 视频控制器vga兼容是什么(视频控制器vga兼容感叹号)

    视频控制器vga兼容是什么(视频控制器vga兼容感叹号)

  • m5a78l华硕(m5a78l华硕接线教程)

    m5a78l华硕(m5a78l华硕接线教程)

  • 微信接不到消息也发不出去是怎么回事(微信接不到消息了是什么情况)

    微信接不到消息也发不出去是怎么回事(微信接不到消息了是什么情况)

  • 运算器的完整功能是什么(运算器的完整功能是进行)

    运算器的完整功能是什么(运算器的完整功能是进行)

  • aux接口可以插麦克风吗(aux接口可以接无线话筒吗)

    aux接口可以插麦克风吗(aux接口可以接无线话筒吗)

  • mate30pro比p30pro拍照谁强(华为mate30pro和华为p30pro拍照哪个好)

    mate30pro比p30pro拍照谁强(华为mate30pro和华为p30pro拍照哪个好)

  • 苹果手机能给mp3下歌吗(苹果手机能给mp3传歌吗)

    苹果手机能给mp3下歌吗(苹果手机能给mp3传歌吗)

  • 电脑强制关机按什么键(电脑强制关机按哪个键子)

    电脑强制关机按什么键(电脑强制关机按哪个键子)

  • 怎么消除电话号码标记(怎么消除电话号码卡)

    怎么消除电话号码标记(怎么消除电话号码卡)

  • iaf认证是什么意思(iaf认证查询)

    iaf认证是什么意思(iaf认证查询)

  • 奥林巴斯使用教程(奥林巴斯chf)

    奥林巴斯使用教程(奥林巴斯chf)

  • 支付宝音箱能连微信吗(支付宝音箱连蓝牙码是什么)

    支付宝音箱能连微信吗(支付宝音箱连蓝牙码是什么)

  • 苹果备忘录怎么看字数(苹果备忘录怎么导出来长图文)

    苹果备忘录怎么看字数(苹果备忘录怎么导出来长图文)

  • 货拉拉允许跟车几人(货拉拉允许跟车几个人)

    货拉拉允许跟车几人(货拉拉允许跟车几个人)

  • 京东怎么删自己评价(京东如何删除自己的购物记录)

    京东怎么删自己评价(京东如何删除自己的购物记录)

  • 华为微信铃声怎么设置(华为微信铃声怎么设置不跟随系统)

    华为微信铃声怎么设置(华为微信铃声怎么设置不跟随系统)

  • 企业所得税的征收对象是什么
  • 城建税是什么税率
  • 股权转让需要缴纳企业所得税吗
  • 一个人可以做多少家公司法人
  • 海外工作薪资比国内高多少
  • 其他项目工会筹备金怎么报税
  • 中药饮片增值税税率
  • 出口货物进项税怎样处理
  • 债务清偿如何进行税务处理
  • 预付账款改为什么科目
  • 货物尾款优惠如何计算
  • 何为自然人股东
  • 社保的计提和缴纳
  • 企业出售房屋交什么税
  • 停车场吗
  • 增值税普通发票有什么用
  • 拿到农产品0税率的发票可以抵扣吗
  • 代开的发票没有打印怎么作废
  • 增值税减免了,附加税申报表还用填吗
  • 旅行社确定收入毛利成本怎么确定?
  • 非专利技术转让合同印花税
  • 不能抵扣的进项发票怎么做分录
  • 存货跌价准备用账面余额还是账面价值
  • 往来款核销需要如何确认
  • 税盘服务费抵税分录
  • 企业在运行过程中会遇到哪些法律问题
  • 企业取得财政拨款怎么算
  • 电脑重装系统启动
  • php比较大小的函数
  • 房地产企业困境
  • u盘无法格式化为NTFS
  • php查看变量数据类型
  • 发财树叶子发黄怎样补救
  • php怎么输出中文
  • redis面试必会6题经典
  • diff比较文件不同输出
  • 会计分录什么时候写主营业务收入
  • php采集器
  • 独立核算的分公司可以汇总纳税吗
  • 借条这样写才有效
  • 法人与财务负责人不应为同一人
  • 职工短期薪酬包括哪些
  • 销项负数发票能作废吗
  • 单位财务报销制度和流程
  • 增值税附加税的计算基数
  • 本月增加的无形资产本月摊销
  • 代销产品如何做会计分录
  • 建筑安装业,指从事____的企业
  • 政府补助的分类及会计处理方法有哪些
  • 小企业会计制度废止
  • 公司租赁个人车辆租金多少合适
  • 水利基金和印花税的计税依据一样吗
  • 代开发票预缴税款的比例是多少呢?
  • 开出发票后直接做账吗?
  • 其他业务收入与其他业务成本的关系
  • 简要分析企业购买行为的影响因素
  • 会计凭证用什么纸打印
  • 用于职工福利的增值税怎么计算
  • 劳动者权益包含哪些
  • mysql数据库增加列
  • VMware虚拟机安装Android系统
  • bios如何设置关机键开机
  • 安装xp后win7不能用怎么办
  • win7系统怎么设置屏幕常亮
  • linux修改服务器ip地址
  • isignup.exe是什么进程 isignup进程查询
  • win7怎样设置屏幕保护密码
  • unity2d横版游戏源码
  • opengl粒子系统烟花
  • Unity Spine Skeleton Animation 2D骨骼动画 For Game 介绍
  • 安装两个linux
  • vue源码是用什么写的
  • Node.js中的事件循环是什么意思
  • centos 安装完成后无法启动
  • js实现点击按钮移动滑块到另一个按钮的方法
  • python多进程模块
  • python环境及基础语法
  • python else用法
  • 专项附加扣除修改后,多扣的税
  • 山西省税务
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设