vue常見路由跳轉(zhuǎn)的幾種方式小結(jié)
常見的路由跳轉(zhuǎn)有以下四種:
1. <router-link to="跳轉(zhuǎn)路徑">
/* 不帶參數(shù) */
<router-link :to="{name:'home'}">
<router-link :to="{path:'/home'}">
// 更建議用name
// router-link鏈接中,帶'/' 表示從根路徑開始,不帶 表示從當前路由開始
/* 帶參 */
// 1. params傳參
<router-link :to="{name:'home', params: {id:1}}">
// 路由配置 用 path: "/home/:id" 或者 path: "/home:id"
// 配置path,刷新頁面參數(shù)保留;不配置path,刷新頁面后 參數(shù)會消失
// html 取參 $router.params.id
// script 取參 this.$router.params.id
// 2 query傳參
<router-link :to="{name:'home', query: {id:1}}">
// 路由可不配置
// html 取參 $router.query.id
// script 取參 this.$router.query.id也可,如

2. this.$router.push() 跳轉(zhuǎn)到指定的url,并在history中添加記錄(即,點擊回退,會退回上一個頁面)。
/* 不傳參 或 query傳參 */
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})
this.$router.push({name:'home', query: {id:'1'}})
this.$router.push({path:'/home', query: {id:'1'}})
/* params傳參 */
this.$router.push({name:'home', params: {id:'1'}})
this.$router.push({path:'/home', params: {id:'1'}})
//注: params傳參只能用name
//配置path,刷新頁面參數(shù)保留;不配置path,刷新參數(shù)消失
/* query和params的區(qū)別 */
// query類似于 get 請求,跳轉(zhuǎn)后頁面url會拼接參數(shù),如?id=1。-->適用于 非重要的參數(shù)
// params 類似于post,不拼接參數(shù),刷新頁面后參數(shù)消失。--->適用于 安全性較高的參數(shù),如密碼
html中,如:

3. this.$router.replace() 跳轉(zhuǎn)到指定的url, 但不會在history記錄(即,點擊回退 退回到上上個頁)
4. this.$router.go(n) 向前或向后跳轉(zhuǎn) n 個頁面,n可正可負。
使用后三種 需要在<script setup>中 導(dǎo)入
import router from '@/router';
參考:路由之間跳轉(zhuǎn)的方式_路由跳轉(zhuǎn)幾種方式_時間管理maste的博客-CSDN博客
路由跳轉(zhuǎn)幾種方法_路由跳轉(zhuǎn)的方式有哪幾種_丶凡人的博客-CSDN博客
到此這篇關(guān)于vue常見路由跳轉(zhuǎn)的幾種方式小結(jié)的文章就介紹到這了,更多相關(guān)vue路由跳轉(zhuǎn) 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue iview實現(xiàn)動態(tài)路由和權(quán)限驗證功能
這篇文章主要介紹了vue iview實現(xiàn)動態(tài)路由和權(quán)限驗證功能,動態(tài)路由控制分為兩種:一種是將所有路由數(shù)據(jù)存儲在本地文件中,另一種則是本地只存儲基本路由,具體內(nèi)容詳情大家參考下此文2018-04-04
vue-electron使用serialport時問題解決方案
這篇文章主要介紹了vue-electron使用serialport時問題解決方案,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09
Vue中directive的鉤子函數(shù)(bind,inserted等)用法及使用說明
Vue指令鉤子函數(shù)(bind、inserted等)用于在不同階段操作元素,bind綁定時初始化,inserted插入DOM后執(zhí)行,update值變化時觸發(fā),componentUpdated全部更新后調(diào)用,unbind解綁時清理,根據(jù)需求選擇對應(yīng)鉤子實現(xiàn)功能增強2025-07-07

