Vue跳轉(zhuǎn)頁面的幾種常用方法總結
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中鏈接如果是'/'開始就是從根路由開始;如果不帶'/',則從當前路由開始。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.id3)帶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.id3)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.id4)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.id3)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.id5.this.$router.go(n)
<button @click="upPage">[上一頁]</button>
<button @click="downPage">[下一頁]</button>
upPage() {
this.$router.go(-1); // 后退一步記錄,等同于 history.back()
},
downPage() {
this.$router.go(1); // 在瀏覽器記錄中前進一步,等同于 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)到上個頁面 (直接替換當前頁面)。this.$router.go(n)
向前或者向后跳轉(zhuǎn)n個頁面,n可為正整數(shù)或負整數(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>標簽
a標簽可以跳轉(zhuǎn)外部鏈接,不能路由跳轉(zhuǎn)
<a rel="external nofollow" ><button>點擊跳轉(zhuǎn)5</button></a>
9.<iframe>標簽
想要在Vue應用中內(nèi)嵌一個外部網(wǎng)頁,可以使用<iframe>標簽:
<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>標簽的src屬性設置為要嵌入的外部網(wǎng)頁的URL
可以調(diào)整width和height屬性來設置iframe的大小
frameborder屬性用于設置是否顯示邊框,設置為"0"表示不顯示邊框
以上就是Vue跳轉(zhuǎn)頁面的幾種常用方法總結的詳細內(nèi)容,更多關于Vue跳轉(zhuǎn)頁面的資料請關注腳本之家其它相關文章!
相關文章
vue在table表中懸浮顯示數(shù)據(jù)及右鍵菜單
這篇文章主要為大家詳細介紹了vue在table表中懸浮顯示數(shù)據(jù)及右鍵菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Element實現(xiàn)動態(tài)增加多個輸入框并校驗
本文主要介紹了Element實現(xiàn)動態(tài)增加多個輸入框并校驗,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07
Vue實現(xiàn)未登錄跳轉(zhuǎn)到登錄頁的示例代碼
本文主要介紹了Vue實現(xiàn)未登錄跳轉(zhuǎn)到登錄頁的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08

