亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue router錯(cuò)誤跳轉(zhuǎn)到首頁("/")的問題及解決

 更新時(shí)間:2023年10月07日 10:25:24   作者:CodingBugs  
這篇文章主要介紹了Vue router錯(cuò)誤跳轉(zhuǎn)到首頁("/")的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Vue router錯(cuò)誤跳轉(zhuǎn)到首頁("/")

Vue通過this.$router.push方法進(jìn)行頁面的跳轉(zhuǎn)主要有兩種方式

// router/index.js
const routes = [{
?? ??? ?path: "/",
?? ??? ?name: "login",
?? ??? ?component: Login
?? ?}, {?
?? ??? ?path: "/index",
?? ??? ?name: "index"
?? ??? ?component: Index
?? ?}
]
①this.$router.push({path: "/index", query: {id: "1", name: "one"}})
②this.$router.psuh({name: "login", params: {id: "2", name: "two"}})

兩種方法一個(gè)指定路徑,一個(gè)指定組件名

path對(duì)應(yīng)router中path, name對(duì)應(yīng)router中的name,一一對(duì)應(yīng)

path配合query, name配合params

如果path指定的路徑在router/index.js中不存在地址欄會(huì)跳轉(zhuǎn)但無法加載出內(nèi)容

而name指定的名稱在router/index.js中不存在會(huì)導(dǎo)致默認(rèn)跳轉(zhuǎn)到"/"

Vue router 路由跳轉(zhuǎn)方法概述

一、概述

使用到Vue的項(xiàng)目,我們最常見使用的就是Vue配套的Vue Router庫。

那么在平日開發(fā)中,有多少種跳轉(zhuǎn)路由的方法?

二、跳轉(zhuǎn)方法

1、使用router-link標(biāo)簽

使用router-link標(biāo)簽,我們通常會(huì)使用到2個(gè)參數(shù),最常用的就是to參數(shù)

to參數(shù),表示你想要跳轉(zhuǎn)到的路由對(duì)象

router-link標(biāo)簽,會(huì)調(diào)用router.push()方法,該方法會(huì)在你點(diǎn)擊瀏覽器會(huì)退按鈕的時(shí)候,無痕回退一個(gè)路由。

可以是路由路徑

<router-link to="/home">Home</router-link>
<router-link :to="'/home'">Home</router-link>

也可以是路由對(duì)象,甚至還可以為其攜帶參數(shù)

<router-link :to="{ path: '/home' }">Home</router-link>
<router-link :to="{ name: 'user', params: { userId: '123' }}">User</router-link>
<router-link :to="{ path: '/register', query: { plan: 'private' }}">
? Register
</router-link>

2、使用router-replace

設(shè)置 replace 屬性的話,當(dāng)點(diǎn)擊時(shí),會(huì)調(diào)用 router.replace(),而不是 router.push(),所以導(dǎo)航后不會(huì)留下歷史記錄。

<router-link to="/abc" replace></router-link>

3、使用router-push

方法1和2是使用html的方法來調(diào)用,對(duì)應(yīng)的,也有使用js代碼來控制路由的方法

router.push('/users/eduardo')
router.push({ path: '/users/eduardo' })
router.push({ name: 'user', params: { username: 'eduardo' } })
router.push({ path: '/register', query: { plan: 'private' } })
router.push({ path: '/about', hash: '#team' })

三、路由中params和query的區(qū)別

在上述代碼中, 發(fā)現(xiàn)給路由傳遞參數(shù),有params和query兩個(gè)不同的方式,他們的區(qū)別又是什么 ?

query是什么?

從 URL 的 search 部分提取的已解碼查詢參數(shù)的字典。就是地址中?后面的內(nèi)容,不過是已經(jīng)解析的。

params是什么?

從 path 中提取的已解碼參數(shù)字典。就是vue路由中的路徑參數(shù)

如下方代碼中的**id **字段,就是路徑參數(shù),當(dāng)使用params的時(shí)候,就可以獲取到。

const routes = [
? { path: '/users/:id', component: User },
]

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論