[Vue Router] Programmatic Navigation
Navigation is triggered using Vue Router when a <router-link>
is clicked, but also can be triggered programmatically from inside our code. In this lesson we’ll look deeper into how this can work. Let me show you where this would be needed, before i show you the new syntax.
✅ Solution: router.push
The solution here is a call to router.push
, with the arguments the same as when we use <router-link>
. So in our case:
<script setup>
import { useRouter } from 'vue-router'
defineProps(['event'])
const router = useRouter()
const register = () => {
// If registered then redirect to event details
router.push({
name: 'EventDetails'
})
}
</script>
<template>
<p>Regstration form here</p>
<button @click="register">Register Me!</button>
</template>
Other Push Examples
When you click on a <router-link>
it’s simply calling the router.push
function inside the code. So as you can imagine, there’s all sorts of combinations you can use just like <router-link>
:
// Directly to the path with a single string
router.push('/about')
// Directly to the path with an object
router.push({ path: '/about' })
// Directly to the named path
router.push({ name: 'About' })
// With a dynamic segment as I showed above
router.push({ name: 'EventDetails', params: { id: 3 } })
// With a query ... resulting in /?page=2 in our example app
router.push({ name: 'EventList', query: { page: 2 } })
The params and query values can be variables, as with our example above.
Navigation & Replace
There are times when you might want to navigate the user away from a page, while not pushing a new history entry in their browser (effectively rendering the back button useless for returning to the current page). Perhaps the form you’re submitting, once submitted, should not be allowed to submit again? Beware: This could really confuse your users. 🤣
router.replace({ name: 'EventDetails', params: { id: 3 } })
This will replace
the current page I’m on with the EventDetails page, so the back button will no longer go back to the current page. The arguments are the same as router.push
.
Navigating the History Stack
Maybe you’d like to have a custom back & forward button in your interface. This might be useful if you’re building a native mobile app that doesn’t have the same browser bars. In this case you can use go
to navigate forward and back in history.
// Go forward a record
router.go(1)
// Go backward a record
router.go(-1)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2022-11-26 [XState] raise event: trigger an action immediately when reach target state
2022-11-26 [Typescript] 118. Hard - IsRequiredKey
2022-11-26 [Typescript] 117. Hard - ClassPublicKeys
2020-11-26 [RxJS] Filtering operator: first
2020-11-26 [Tools] Interactively exploring a large JSON file with fx
2020-11-26 [Javascript] Broadcaster + Operator + Listener pattern -- 22. mapError, wrap fetch with broadcaster with cancellation
2020-11-26 [Javascript] Cancel a promise