Vue跳轉頁面的幾種常用方法總結
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: '調度字典數據', icon: '' }
}
]
},2. router-link跳轉
1)不帶參數
<router-link :to="{name:'home'}">
<router-link :to="{path:'/home'}"> //name,path都行, 建議用name
// 注意:router-link中鏈接如果是'/'開始就是從根路由開始;如果不帶'/',則從當前路由開始。2)帶params參數
<router-link :to="{name:'home', params: {id:10001}}">
// params傳參數 (類似post)
// 路由配置 path: "/home/:id" 或者 path: "/home:id"
// 不配置path ,第一次可請求,刷新頁面id會消失;配置path,刷新頁面id會保留。
// html 取參 $route.params.id script 取參 this.$route.params.id3)帶query參數
<router-link :to="{name:'dispatchDict', query: {id:10001}}">
// query傳參數 (類似get,url后面會顯示參數)
// 路由可不配置
// html 取參 $route.query.id script 取參 this.$route.query.id<!-- 帶參數跳轉 -->
<router-link :to="{path:'/dispatchDict/type/data',query:{setid:123456}}">
<button>點擊跳轉1</button>
</router-link>3. this.$router.push()
1)不帶參數
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, 跳轉之后頁面url后面會拼接參數,類似?id=123456, 非重要性的可以這樣傳, 密碼之類還是用params刷新頁面id還在 params類似post, 跳轉之后頁面url后面不會拼接參數, 但是刷新頁面id會消失。
4. this.$router.replace()
1)不帶參數
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
跳轉到指定url路徑,并向history棧中添加一個記錄,點擊后退會返回到上一個頁面。this.$router.replace
跳轉到指定url路徑,但是history棧中不會有記錄,點擊返回會跳轉到上個頁面 (直接替換當前頁面)。this.$router.go(n)
向前或者向后跳轉n個頁面,n可為正整數或負整數。
7.目的頁面動態(tài)刷新
參數不同,跳轉到同一頁面,會面臨頁面數據刷新問題。
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標簽可以跳轉外部鏈接,不能路由跳轉
<a rel="external nofollow" ><button>點擊跳轉5</button></a>
9.<iframe>標簽
想要在Vue應用中內嵌一個外部網頁,可以使用<iframe>標簽:
<template>
<div>
<!-- 在這里嵌入外部網頁 -->
<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屬性設置為要嵌入的外部網頁的URL
可以調整width和height屬性來設置iframe的大小
frameborder屬性用于設置是否顯示邊框,設置為"0"表示不顯示邊框
以上就是Vue跳轉頁面的幾種常用方法總結的詳細內容,更多關于Vue跳轉頁面的資料請關注腳本之家其它相關文章!
相關文章
Vue?Router中router.resolve方法使用實例
這篇文章主要給大家介紹了關于Vue?Router中router.resolve方法使用的相關資料,router.resolve方法在前端路由庫中用于解析路由信息,接受路徑或路由對象,返回解析后的?URL、路由和位置對象,需要的朋友可以參考下2024-11-11
使用webpack打包后的vue項目如何正確運行(express)
這篇文章主要介紹了使用webpack打包后的vue項目如何正確運行(express) ,接下來通過本文給大家詳細介紹,需要的朋友可以參考下2018-10-10

