位置: 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框架结构)

  • 小规模纳税人销售农产品税率是多少
  • 减免税额和免税额一样吗
  • 国有独资企业是国企吗
  • 采购人员的差旅费计入采购成本吗?
  • 库存商品余额在借方
  • 利润总额和未分配利润的公式
  • 贸易公司的印花税税率是多少
  • 机动车发票可以红冲重开吗
  • 税控维护费的分录
  • 案例讲解:将自己的房产用于办公使用,在税收的缴纳中该如何把控?
  • 营业额和营业收入怎么填写
  • 审计调账后企业怎么处理
  • 记账凭证需要哪些人员签章
  • 土地增值税地价扣除
  • 增值税专用发票税号错误
  • 如何修改windows11开机密码
  • 询证函是什么文书
  • 开成品油发票要注意什么?
  • 会计如果做假账
  • 旅客购买电子客票
  • 批量删除 超链接
  • windows 11什么时候
  • 企业所得税的纳税人包括哪些
  • 大城遗址公园
  • 购买性支出和转移性支出的本质区别
  • mac vue搭建本地环境
  • 废旧物资回收企业所得税优惠政策
  • 勃朗峰高度
  • redis如何实现分布式事务
  • 确认收入需要哪些资料
  • 准确率精确率
  • ubuntu系统删除
  • ps命令显示进程状态
  • 会计调整以前年度遗留问题查不出来说明怎么写
  • 农业合作社需要纳税吗
  • 视同销售在纳税明细表中怎么填
  • 3月1日前包括什么意思
  • 如何对php网站页面进行修改
  • mysql字符集详解
  • 工厂杂工工资
  • 小规模企业免征增值税如何申报
  • 应付账款坏账损失的会计分录
  • 交易性金融资产入账价值怎么计算
  • 分组 sql
  • 不动产登记机构应当履行下列职责?
  • 筹建期的收入要交企业所得税吗
  • 增值税月末结转处理
  • 毛利差怎么计算公式
  • 营改增后不动产转让增值税
  • 转入企业银行存款利息分录
  • 将税后利润首先用于增加投资
  • 固定资产竣工前予以资本化吗
  • 金蝶k3怎么新增会计科目
  • 购买农产品进行销售要交税吗
  • 企业一般账户开户申请理由
  • sqlserver sql日志
  • win8怎么看windows
  • win8系统运行慢怎么办
  • windows域环境搭建
  • mac中通过python关闭浏览器中的finder弹框
  • hptasks.exe是病毒吗 是什么进程 hptasks进程说明
  • win7系统共享打印机设置方法
  • ims文件是什么意思
  • kcleaner是什么文件夹
  • 防止暴力破解的方法
  • windows8使用技巧
  • win8怎么设置自启动
  • win7系统怎么查看内存
  • 判断文件是否存在 java
  • jquery模拟表单提交
  • nodejs中向HTTP响应传送进程的输出
  • js面向对象面试题
  • js操作属性的方法
  • python列表组成字符串
  • 变更法人需要法人本人去吗
  • 税务局网上缴税
  • 国家税务总局网址
  • 湖北省叉车考试题库
  • 红伞伞儿歌寓意着什么
  • 2021年社保又涨价了
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设