欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue跳轉頁面的幾種常用方法總結

 更新時間:2024年09月24日 11:35:31   作者:Best_Liu~  
在Vue.js中,頁面跳轉是構建單頁面應用(SPA)的基本操作之一,本文將介紹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.id

3)帶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.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, 跳轉之后頁面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.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);   // 在瀏覽器記錄中前進一步,等同于 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

可以調整widthheight屬性來設置iframe的大小

frameborder屬性用于設置是否顯示邊框,設置為"0"表示不顯示邊框

以上就是Vue跳轉頁面的幾種常用方法總結的詳細內容,更多關于Vue跳轉頁面的資料請關注腳本之家其它相關文章!

相關文章

  • vue3+vite應用中添加sass預處理器問題

    vue3+vite應用中添加sass預處理器問題

    這篇文章主要介紹了vue3+vite應用中添加sass預處理器問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Vue和Bootstrap的整合思路詳解

    Vue和Bootstrap的整合思路詳解

    這篇文章主要介紹了Vue和Bootstrap的整合思路詳解,需要的朋友可以參考下
    2017-06-06
  • 詳解vite2.0配置學習(typescript版本)

    詳解vite2.0配置學習(typescript版本)

    這篇文章主要介紹了詳解vite2.0配置學習(typescript版本),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • 如何使用Vue+Element做個個人中心

    如何使用Vue+Element做個個人中心

    我們在做了用戶登錄后,就會讓用戶跳轉到個人中心,下面這篇文章主要給大家介紹了關于如何使用Vue+Element做個個人中心的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-06-06
  • Vue?Router中router.resolve方法使用實例

    Vue?Router中router.resolve方法使用實例

    這篇文章主要給大家介紹了關于Vue?Router中router.resolve方法使用的相關資料,router.resolve方法在前端路由庫中用于解析路由信息,接受路徑或路由對象,返回解析后的?URL、路由和位置對象,需要的朋友可以參考下
    2024-11-11
  • 使用webpack打包后的vue項目如何正確運行(express)

    使用webpack打包后的vue項目如何正確運行(express)

    這篇文章主要介紹了使用webpack打包后的vue項目如何正確運行(express) ,接下來通過本文給大家詳細介紹,需要的朋友可以參考下
    2018-10-10
  • vue頁面切換項目實現轉場動畫的方法

    vue頁面切換項目實現轉場動畫的方法

    這篇文章主要介紹了vue頁面切換項目實現轉場動畫的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • vue開發(fā)調試神器vue-devtools使用詳解

    vue開發(fā)調試神器vue-devtools使用詳解

    這篇文章主要為大家詳細介紹了vue開發(fā)調試神器vue-devtools的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 詳解vue-cli 本地開發(fā)mock數據使用方法

    詳解vue-cli 本地開發(fā)mock數據使用方法

    這篇文章主要介紹了詳解vue-cli 本地開發(fā)mock數據使用方法,如果后端接口尚未開發(fā)完成,前端開發(fā)一般使用mock數據。小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • vue父子組件動態(tài)傳值的幾種方式及注意問題詳解

    vue父子組件動態(tài)傳值的幾種方式及注意問題詳解

    這篇文章主要介紹了vue父子組件動態(tài)傳值的幾種方式及注意問題詳解,需要的朋友可以參考下
    2022-12-12

最新評論