vue跳轉頁面的幾種方法(推薦)
vue跳轉不同頁面的多種方法
1:router-link跳轉
<!-- 直接跳轉 --> <router-link to='/testDemo'> <button>點擊跳轉2</button> </router-link> <!-- 帶參數(shù)跳轉 --> <router-link :to="{path:'testDemo',query:{setid:123456}}"> <button>點擊跳轉1</button> </router-link> <router-link :to="{name:'testDemo',params:{setid:1111222}}"> <button>點擊跳轉3</button> </router-link>
2:this.$router.push()
<template> <div id='test'> <button @click='goTo()'>點擊跳轉4</button> </div> </template> <script> export default{ name:'test', methods:{ goTo(){ //直接跳轉 this.$router.push('/testDemo'); //帶參數(shù)跳轉 this.$router.push({path:'/testDemo',query:{setid:123456}}); this.$router.push({name:'testDemo',params:{setid:111222}}); } } } </script>
params和query傳參數(shù)有什么不一樣??在地址欄中可以看到,params傳參數(shù)時,地址欄中看不到參數(shù)的內容,有點像ajax中的post傳參,query傳參數(shù)時,地址欄中可以看到傳過來的參數(shù)信息,有點像ajax的個體傳參
如果單獨傳setId一個參數(shù)的時候,地址欄中的地址如下圖:
第一種方式:path - query 傳參
第二種方式:name - params傳參數(shù)
但是一般情況下,傳參數(shù)是傳遞一個對象,當傳遞的是一個對象的時候,地址欄中的地址如下圖:
第一種方式:path - query 傳參
第二種方式:name - params傳參數(shù)
3:a標簽可以跳轉么??可以跳轉外部鏈接,不能路由跳轉
<a href="https://www.baidu.com"><button>點擊跳轉5</button></a>
接收方怎么接收參數(shù)??this.$route.query.serid和this.$route.params.setid
,以下舉一個接收的例子
注意接收參數(shù)時是 $route 不是 $router
<template> <div> testDemo{{this.$route.query.setid}} </div> </template>
知識點補充:vue三種不同方式實現(xiàn)頁面跳轉
Vue:router-lin
<router-link to="/">[跳轉到主頁]</router-link> <router-link to="/login">[登錄]</router-link> <router-link to="/logout">[登出]</router-link>
this.$router.push("/");
<button @click="goHome">[跳轉到主頁]</button> export default { name: "App", methods: { // 跳轉頁面方法 goHome() { this.$router.push("/"); }, }
this.$router.go(1);
<button @click="upPage">[上一頁]</button> <button @click="downPage">[下一頁]</button> upPage() { // 后退一步記錄,等同于 history.back() this.$router.go(-1); }, downPage() { // 在瀏覽器記錄中前進一步,等同于 history.forward() this.$router.go(1); }
代碼示例:
<template> <div id="app"> <img src="./assets/logo.png"> <router-view/> <router-link to="/">[跳轉到主頁]</router-link> <router-link to="/login">[登錄]</router-link> <router-link to="/logout">[登出]</router-link> <!-- javascript跳轉頁面 --> <button @click="goHome">[跳轉到主頁]</button> <!-- 回到上一頁 --> <button @click="upPage">[上一頁]</button> <button @click="downPage">[下一頁]</button> <!-- 回到下一頁 --> </div> </template> <script> export default { name: "App", methods: { // 跳轉頁面方法 goHome() { this.$router.push("/"); }, upPage() { // 后退一步記錄,等同于 history.back() this.$router.go(-1); }, downPage() { // 在瀏覽器記錄中前進一步,等同于 history.forward() this.$router.go(1); } } }; </script>
總結
到此這篇關于vue不同方法跳轉頁面的幾種方法的文章就介紹到這了,更多相關vue 跳轉頁面內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于vue+ bootstrap實現(xiàn)圖片上傳圖片展示功能
這篇文章主要介紹了基于vue+ bootstrap實現(xiàn)圖片上傳圖片展示功能,需要的朋友可以參考下2017-05-05vue打包部署到springboot并通過tomcat運行的操作方法
這篇文章主要介紹了vue打包部署到springboot并通過tomcat運行的操作方法,本文通過實例圖文并茂的形式給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-05-05vue項目使用typescript創(chuàng)建抽象類及其使用方式
這篇文章主要介紹了vue項目使用typescript創(chuàng)建抽象類及其使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03vue中數(shù)據(jù)字典dicts的簡單說明和用法介紹
這篇文章主要給大家介紹了關于vue中數(shù)據(jù)字典dicts的簡單說明和用法的相關資料,如果您想在Vue中使用字典查詢,您可以使用Vue的計算屬性和方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-01-01解決vue admin element noCache設置無效的問題
今天小編就為大家分享一篇解決vue admin element noCache設置無效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11