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

  • 网络推广方法大全(100种网络推广方法)

    网络推广方法大全(100种网络推广方法)

  • 华为可以设置悬浮快门键吗(华为设置悬浮窗)

    华为可以设置悬浮快门键吗(华为设置悬浮窗)

  • 电信宽带怎么网上注销(电信宽带怎么网上报停)

    电信宽带怎么网上注销(电信宽带怎么网上报停)

  • 华为荣耀畅玩9x有人脸识别吗(华为荣耀畅玩9x参数配置详情)

    华为荣耀畅玩9x有人脸识别吗(华为荣耀畅玩9x参数配置详情)

  • 华为9xpro的上市时间(华为9xpro手机怎么样)

    华为9xpro的上市时间(华为9xpro手机怎么样)

  • 在windows7中将中文输入方式切换到英文(在windows7中下列说法正确的是)

    在windows7中将中文输入方式切换到英文(在windows7中下列说法正确的是)

  • 手机怎么发送短信(手机怎么发送短信到指定号码)

    手机怎么发送短信(手机怎么发送短信到指定号码)

  • iphone为什么sim卡会出现无服务的现象(iphone为什么sim卡会出现无效)

    iphone为什么sim卡会出现无服务的现象(iphone为什么sim卡会出现无效)

  • 芒果TV账号信息查找方法(芒果tv账号信息异常)

    芒果TV账号信息查找方法(芒果tv账号信息异常)

  • iphone11防窥屏哪里设置(苹果11怎么开防窥探)

    iphone11防窥屏哪里设置(苹果11怎么开防窥探)

  • icloud合并还是不合并(icloud合并与不合并区别)

    icloud合并还是不合并(icloud合并与不合并区别)

  • 陌陌已经实名认证了为什么还是异常(陌陌已经实名认证了)

    陌陌已经实名认证了为什么还是异常(陌陌已经实名认证了)

  • 华为mate30pro要更新系统吗(华为mate30pro需要更新吗)

    华为mate30pro要更新系统吗(华为mate30pro需要更新吗)

  • 电脑没有dp接口怎么办(电脑没有dp接口华为vr)

    电脑没有dp接口怎么办(电脑没有dp接口华为vr)

  • 华为荣耀9x微信视频怎么美颜(华为荣耀9X微信分身在哪里)

    华为荣耀9x微信视频怎么美颜(华为荣耀9X微信分身在哪里)

  • qq视频被录屏会有提示吗(qq视频录屏会被对方发现吗)

    qq视频被录屏会有提示吗(qq视频录屏会被对方发现吗)

  • 打字声音怎么设置(打字声音怎么设置 手机键盘打字声音怎么设置)

    打字声音怎么设置(打字声音怎么设置 手机键盘打字声音怎么设置)

  • 苹果11pro有刘海吗(苹果11pro有没有刘海)

    苹果11pro有刘海吗(苹果11pro有没有刘海)

  • 手机怎么申请邮箱账号(手机怎么申请邮箱免费注册)

    手机怎么申请邮箱账号(手机怎么申请邮箱免费注册)

  • oppo添加桌面小部件(oppo添加桌面小组件)

    oppo添加桌面小部件(oppo添加桌面小组件)

  • 抖音企业认证需要什么条件(抖音企业认证需要多久)

    抖音企业认证需要什么条件(抖音企业认证需要多久)

  • 被淘宝店铺拉黑的表现(被淘宝店铺拉黑影响在其它店铺购买吗)

    被淘宝店铺拉黑的表现(被淘宝店铺拉黑影响在其它店铺购买吗)

  • 苹果手机不能横屏了是什么原因(苹果手机不能横屏在哪里设置)

    苹果手机不能横屏了是什么原因(苹果手机不能横屏在哪里设置)

  • oppor17耳机模式怎么取消(oppoa72耳机模式)

    oppor17耳机模式怎么取消(oppoa72耳机模式)

  • 华为mate9小声解决方法(mate9的声音小怎么办)

    华为mate9小声解决方法(mate9的声音小怎么办)

  • Windows server——部署web服务(windowsserver2016激活密钥)

    Windows server——部署web服务(windowsserver2016激活密钥)

  • 小规模企业所得税计算
  • 无形资产摊销的会计科目
  • 超过库存现金限额的现金要及时存入银行
  • 百旺税控盘自己用不了
  • 销售购物取得的收入
  • 房地产开发企业会计科目
  • 辞退员工补偿金账务处理
  • 土地股权转让需交什么税
  • 递延所得税当期发生额
  • 进项税转出企业所得税账务怎么处理
  • 小规模纳税人咨询费税率
  • 完全成本法下期间费用应当包括
  • 向境外支付违约金要代扣税吗
  • 微信转账的手续费规则
  • 汇算清缴是什么时间
  • php编程代码
  • 消防设施费用怎么入账
  • 分包方可以简易计税吗
  • 资产收购的账务处理
  • php数组有哪几种类型
  • 很有意思的一段话
  • axios提交文件
  • http://与www.开头的网站有何区别
  • php similar_text()函数的定义和用法
  • pytorch训练模型计算f1
  • 图像的分类方法及具体的分类
  • transformerss
  • addr指令
  • 帝国cms wordpress
  • 发现以前年度损益调整怎么记账
  • mysql中事件的作用
  • mysql建表的完整步骤
  • 织梦开发教程
  • 织梦商城网站源码
  • 小微企业可以不交税吗
  • 预付卡账务处理在注会
  • 公司有收入可以做零申报吗
  • 一般纳税人资格证明在哪里查询
  • 在途物资属于会计科目吗
  • 企业所得税会计利润总额计算公式
  • 企业购置房产进项税税率
  • 期权费的合理范围计算
  • 兼职收入用缴纳增值税吗
  • 资产减值准备的计提影响递延所得税资产吗
  • 普通的收据可以入账吗
  • 开发成本怎么做分录
  • 物流辅助服务包含哪些内容
  • 销售退回的会计分录 预计负债
  • 备用金怎么计入明细账
  • 待处理财产损益的账务处理
  • 出售设备账务处理
  • 存货跌价准备的计算
  • 基本户转账法人会知道吗
  • 搭赠商品如何开票
  • sql server索引的使用
  • mysql快照读的实现
  • Navicat for MySQL导出表结构脚本的简单方法
  • Mac怎么更改锁屏密码
  • win10系统怎么设置不锁屏和休眠
  • win10任务界面
  • win8全屏快捷键
  • win7怎么查看电池信息
  • win7蓝屏怎么办
  • linux dns1
  • es6数组函数
  • javascript怎么用
  • cocos onload
  • js中创建对象的几种方法
  • linux命令批量执行
  • 网页的css
  • 以下代码的运行结果是哪一项
  • jquery判断对象是否存在
  • unity脚本api
  • javascript面向对象 第三方类库
  • 什么是跨地区经营汇总纳税企业
  • 企业所得税
  • 天津市东丽区军粮城派出所电话
  • 扶贫绩效目标申请怎么写
  • 贵州省国家税务局电子税务局官网
  • 广东省电子税务局官网
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设