Vue跳轉(zhuǎn)頁面的幾種常用方法總結(jié)
1.路由策略配置
/src/router下配置路由策略
{ path: '/dispatchDict', component: Layout, hidden: true, children: [ { path: 'type/data', component: (resolve) => require(['@/views/dispatch/dict/data'], resolve), name: 'dispatchDict', meta: { title: '調(diào)度字典數(shù)據(jù)', icon: '' } } ] },
2. router-link跳轉(zhuǎn)
1)不帶參數(shù)
<router-link :to="{name:'home'}"> <router-link :to="{path:'/home'}"> //name,path都行, 建議用name // 注意:router-link中鏈接如果是'/'開始就是從根路由開始;如果不帶'/',則從當(dāng)前路由開始。
2)帶params參數(shù)
<router-link :to="{name:'home', params: {id:10001}}"> // params傳參數(shù) (類似post) // 路由配置 path: "/home/:id" 或者 path: "/home:id" // 不配置path ,第一次可請求,刷新頁面id會消失;配置path,刷新頁面id會保留。 // html 取參 $route.params.id script 取參 this.$route.params.id
3)帶query參數(shù)
<router-link :to="{name:'dispatchDict', query: {id:10001}}"> // query傳參數(shù) (類似get,url后面會顯示參數(shù)) // 路由可不配置 // html 取參 $route.query.id script 取參 this.$route.query.id
<!-- 帶參數(shù)跳轉(zhuǎn) --> <router-link :to="{path:'/dispatchDict/type/data',query:{setid:123456}}"> <button>點擊跳轉(zhuǎn)1</button> </router-link>
3. this.$router.push()
1)不帶參數(shù)
this.$router.push('/home') this.$router.push({name:'home'}) this.$router.push({path:'/home'})
2)query傳參
this.$router.push({name:'dispatchDict',query: {id:'10001'}}) this.$router.push({path:'/dispatchDict/type/data',query: {id:'10001'}}) // html 取參 $route.query.id script 取參 this.$route.query.id
3)params傳參
this.$router.push({name:'dispatchDict',params: {id:'10001'}}) // 只能用 name // 路由配置 path: "/home/:id" 或者 path: "/home:id" , // 不配置path ,第一次可請求,刷新頁面id會消失 // 配置path,刷新頁面id會保留 // html 取參 $route.params.id script 取參 this.$route.params.id
4)query和params區(qū)別
query類似get, 跳轉(zhuǎn)之后頁面url后面會拼接參數(shù),類似?id=123456, 非重要性的可以這樣傳, 密碼之類還是用params刷新頁面id還在 params類似post, 跳轉(zhuǎn)之后頁面url后面不會拼接參數(shù), 但是刷新頁面id會消失。
4. this.$router.replace()
1)不帶參數(shù)
this.$router.replace('/home') this.$router.replace({name:'home'}) this.$router.replace({path:'/home'})
2)query傳參
this.$router.replace({name:'dispatchDict',query: {id:'10001'}}) this.$router.replace({path:'/dispatchDict/type/data',query: {id:'10001'}}) // html 取參 $route.query.id script 取參 this.$route.query.id
3)params傳參
this.$router.replace({name:'dispatchDict',params: {id:'10001'}}) // 只能用 name // 路由配置 path: "/home/:id" 或者 path: "/home:id" , // 不配置path ,第一次可請求,刷新頁面id會消失 // 配置path,刷新頁面id會保留 // html 取參 $route.params.id script 取參 this.$route.params.id
5.this.$router.go(n)
<button @click="upPage">[上一頁]</button> <button @click="downPage">[下一頁]</button> upPage() { this.$router.go(-1); // 后退一步記錄,等同于 history.back() }, downPage() { this.$router.go(1); // 在瀏覽器記錄中前進(jìn)一步,等同于 history.forward() }
6.this.$router.push()、 this.$router.replace()、 this.$router.go(n)三者區(qū)別
this.$router.push
跳轉(zhuǎn)到指定url路徑,并向history棧中添加一個記錄,點擊后退會返回到上一個頁面。this.$router.replace
跳轉(zhuǎn)到指定url路徑,但是history棧中不會有記錄,點擊返回會跳轉(zhuǎn)到上個頁面 (直接替換當(dāng)前頁面)。this.$router.go(n)
向前或者向后跳轉(zhuǎn)n個頁面,n可為正整數(shù)或負(fù)整數(shù)。
7.目的頁面動態(tài)刷新
參數(shù)不同,跳轉(zhuǎn)到同一頁面,會面臨頁面數(shù)據(jù)刷新問題。
watch: { // 監(jiān)視dictType變化 "$route.params.dictType": { immediate: true, handler() { const dictType = this.$route.params.dictType; this.getType(dictType); }, }, }, //或者 watch: { // 監(jiān)視dictType變化 "$route.query.dictType": { immediate: true, handler() { const dictType = this.$route.params.dictType; this.getType(dictType); }, }, },
8.<a>標(biāo)簽
a標(biāo)簽可以跳轉(zhuǎn)外部鏈接,不能路由跳轉(zhuǎn)
<a rel="external nofollow" ><button>點擊跳轉(zhuǎn)5</button></a>
9.<iframe>標(biāo)簽
想要在Vue應(yīng)用中內(nèi)嵌一個外部網(wǎng)頁,可以使用<iframe>
標(biāo)簽:
<template> <div> <!-- 在這里嵌入外部網(wǎng)頁 --> <iframe src="https://www.example.com" width="100%" height="500px" frameborder="0"></iframe> </div> </template> <script> export default { name: 'EmbeddedWebPage', } </script> <style scoped> /* 在這里添加樣式 */ </style>
<iframe>
標(biāo)簽的src屬性設(shè)置為要嵌入的外部網(wǎng)頁的URL
可以調(diào)整width
和height
屬性來設(shè)置iframe
的大小
frameborder
屬性用于設(shè)置是否顯示邊框,設(shè)置為"0"表示不顯示邊框
以上就是Vue跳轉(zhuǎn)頁面的幾種常用方法總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Vue跳轉(zhuǎn)頁面的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue3+vite應(yīng)用中添加sass預(yù)處理器問題
這篇文章主要介紹了vue3+vite應(yīng)用中添加sass預(yù)處理器問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02詳解vite2.0配置學(xué)習(xí)(typescript版本)
這篇文章主要介紹了詳解vite2.0配置學(xué)習(xí)(typescript版本),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Vue?Router中router.resolve方法使用實例
這篇文章主要給大家介紹了關(guān)于Vue?Router中router.resolve方法使用的相關(guān)資料,router.resolve方法在前端路由庫中用于解析路由信息,接受路徑或路由對象,返回解析后的?URL、路由和位置對象,需要的朋友可以參考下2024-11-11使用webpack打包后的vue項目如何正確運行(express)
這篇文章主要介紹了使用webpack打包后的vue項目如何正確運行(express) ,接下來通過本文給大家詳細(xì)介紹,需要的朋友可以參考下2018-10-10vue頁面切換項目實現(xiàn)轉(zhuǎn)場動畫的方法
這篇文章主要介紹了vue頁面切換項目實現(xiàn)轉(zhuǎn)場動畫的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11vue開發(fā)調(diào)試神器vue-devtools使用詳解
這篇文章主要為大家詳細(xì)介紹了vue開發(fā)調(diào)試神器vue-devtools的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07詳解vue-cli 本地開發(fā)mock數(shù)據(jù)使用方法
這篇文章主要介紹了詳解vue-cli 本地開發(fā)mock數(shù)據(jù)使用方法,如果后端接口尚未開發(fā)完成,前端開發(fā)一般使用mock數(shù)據(jù)。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05