[Vue Router] Redirect Alias

news/2024/11/29 22:28:49/文章来源:https://www.cnblogs.com/Answer1215/p/18569161

As our application evolves, we may need to change the URL paths of where our pages initially found. There are two convenience methods for this:

 

⚠️ Problem: Changing Routes

What if we needed to change our application from using /about for our about page to /about-us . How might we deal with this?

✅ Solution #1: Redirect

Obviously the first step is to change our original route:

    const router = new VueRouter({routes: [...{path: '/about-us',name: 'About',component: About}]})

If we’re using named routes then we don’t need to change our router-links at all. Otherwise we would have to. Then, since there might be links around the internet to our /about page, we want to make that redirect from /about to /about-us, with the following additional route.

 const router = new VueRouter({routes: [...{ path: '/about', redirect: { name: "About" }}]})

Note we’re using the named route for the redirect. We could have also used redirect: "/about-us" to get the same functionality, but this is hard-coding a URL in one more place we’d have to change if the path changed.

https://firebasestorage.googleapis.com/v0/b/vue-mastery.appspot.com/o/flamelink%2Fmedia%2F1.1607724721632.gif?alt=media&token=9db43660-9344-49aa-bf6b-1f668e679a5a

 

✅ Solution #2: Alias

Instead of redirecting the old path we might just want to alias it, meaning just provide a duplicate path to the same content. We could update that path and provide an alias to the old path:

    const router = new VueRouter({routes: [...{path: '/about-us',name: 'About',component: About,alias: '/about' // <-----}]})

Now the user can go to /about or /about-us and they’ll get the same content.

https://firebasestorage.googleapis.com/v0/b/vue-mastery.appspot.com/o/flamelink%2Fmedia%2F2.1607724721633.gif?alt=media&token=c0696625-f0b0-4c57-9e54-ce727581d101

 

⚠️ Problem: Complex Routes

In the application we’ve been building in our Touring Vue Router course to view an event we go to /event/123. Some developers might prefer this URL to be /events/123, the plural. Let’s assume we want to make this change, and ensure that all our old URLs redirect properly to the new URL.

✅ Solution: Redirecting Dynamic Segments

To redirect a dynamic segment, we’ll need to get access to the params when we create the new path. To do this we’ll need to send in an anonymous function into the redirect property, like so:

📜 /src/router/index.js

...
const routes = [...{path: '/events/:id', // <--- make plural 'events'name: 'EventLayout',...},{path: '/event/:id',redirect: to => {return { name: 'EventDetails', params: { id: to.params.id } }}},

Notice how inside this anonymous function we could do some complex logic if needed. Turns out we can simplify what we wrote above, because the id param will get passed along automatically. Vue Router is smart like this:

{path: '/event/:id',redirect: () => {return { name: 'EventDetails' }}
},

 

✅ Redirect with children

It turns out that redirect has the ability to accept children. So we can do this:

 {path: '/event/:id',redirect: () => {return { name: 'EventDetails' }},children: [{ path: 'register', redirect: () => ({ name: 'EventRegister' }) },{ path: 'edit', redirect: () => ({ name: 'EventEdit' }) }]},

 

✅ Redirect with Wildcard

Another way we could solve this is with a wildcard, like so:

{path: '/event/:afterEvent(.*)',redirect: to => {return { path: '/events/' + to.params.afterEvent }}},

This is taking whatever comes after the matching word /event/ and placing it after /events/. This is less code, and covers all children routes.

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/841135.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据采集实践第四次作业

Gitee作业链接: https://gitee.com/FunkTank/crawl_project/tree/master/作业4 作业① 要求:熟练掌握 Selenium 查找HTML元素、爬取Ajax网页数据、等待HTML元素等内容。 使用Selenium框架+ MySQL数据库存储技术路线爬取“沪深A股”、“上证A股”、“深证A股”3个板块的股票数…

数据采集作业四

数据采集作业四 作业①: 要求: 熟练掌握 Selenium 查找HTML元素、爬取Ajax网页数据、等待HTML元素等内容。 使用Selenium框架+ MySQL数据库存储技术路线爬取“沪深A股”、“上证A股”、“深证A股”3个板块的股票数据信息。 候选网站:东方财富网:http://quote.eastmoney.com/…

Top 5 Kafka使用场景

▫️ 日志处理与分析📈:适用于处理和分析大量日志数据,生成趋势 ▫️ 数据流推荐系统🛒:实时数据流驱动个性化推荐。 ▫️ 系统监控与报警🚨:实时监控系统指标,及时响应报警 ▫️ 变更数据捕获(CDC)🔄:捕获和处理数据库变更,保持系统间数据同步 ▫️ 系…

IDEA2024.3激活提示激活码失效、IDEA2024版本

IDEA激活提示激活码失效解决 点击试用,或者使用网上教程激活后断网打开idea 进入idea随便创建或打开个项目 点击help -> Edit Custom VM Options [帮助 -> 编辑自定义VM选项]编辑自定义VM,鼠标右击找到这个目录看看是不是以前激活的目录,删除 重新执行激活操作 需要…

IDEA2024.3激活提示激活码失效

IDEA激活提示激活码失效解决 点击试用,或者使用网上教程激活后断网打开idea 进入idea随便创建或打开个项目点击help -> Edit Custom VM Options [帮助 -> 编辑自定义VM选项]编辑自定义VM找到这个目录看看是不是以前激活的目录,删除 重新执行激活操作 需要激活软件,可…

使用EVOLVEpro对蛋白质热稳定性进行定向进化

对EVOLVEpro还不熟的读者可以查看之前的博客:Rapid in silico directed evolution by a protein language model with EVOLVEpro 文献解读。 本文通过Kaggle竞赛中提供的酶热稳定性数据集对EVOLVEpro的性能进行验证。 数据集为test.csv和test_labels.csv,这是经过实验测量的某…

计算机概念——io 复用

前言 首先什么是io复用呢? 现在web框架没有不用到io复用的,这点是肯定的,不然并发真的很差。 那么io复用,复用的是什么呢?复用的真的不是io管道啥的,也不是io连接啥的,复用的是io线程。 这其实是操作系统的设计上的io并发不足的地方,然后自己给慢慢填了。 正文 听一段历…

java学习11.25

写了很久的这个差费报销系统,写的太慢了,一半不到,结构上也非常臃肿。

HTML中实现多选一且输入框的启用与禁用

其他类别内容:<script>// 控制"其他类别内容"输入框的启用与禁用function toggleTypeContent() {var typeContentInput = document.getElementById("typecontent");var otherRadio = document.getElementById("other");// 如果选中了&quo…

Pwn 乱刷合集

持续更新中3※ [SHCTF 2024] No stack overflow2考点:ret2libc,libc 库查询,整数溢出在 linux 下使用 checksec 查看该程序开启的保护,发现 Arch 为 amd64-64-little,这说明这是一个 64位 的程序,并且采用了 小端 存储,即低位对应低地址,高位对应高地址。下方的 RELRO …