vue跳轉頁面常用的4種方法與區(qū)別小結
更新時間:2022年03月11日 14:16:38 作者:宋文軒
這篇文章主要給大家介紹了關于vue跳轉頁面常用的4種方法與區(qū)別,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
vue跳轉頁面有好幾種不同方法,下面將通過實例代碼給大家介紹,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下。
1: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:123456}}"> // 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:'home', query: {id:123456}}"> // query傳參數 (類似get,url后面會顯示參數) // 路由可不配置 // html 取參 $route.query.id script 取參 this.$route.query.id
2:this.$router.push()
1. 不帶參數 this.$router.push('/home') this.$router.push({name:'home'}) this.$router.push({path:'/home'}) 2. query傳參 this.$router.push({name:'home',query: {id:'123456'}}) this.$router.push({path:'/home',query: {id:'123456'}}) // html 取參 $route.query.id script 取參 this.$route.query.id 3. params傳參 this.$router.push({name:'home',params: {id:'123456'}}) // 只能用 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會消失。
3. this.$router.replace()
用法同上,和第2個的this.$router.push方法一樣。
4. 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() }
向前或者向后跳轉n個頁面,n可為正整數或負整數
ps : 區(qū)別
this.$router.push
跳轉到指定url路徑,并向history棧中添加一個記錄,點擊后退會返回到上一個頁面。
this.$router.replace
跳轉到指定url路徑,但是history棧中不會有記錄,點擊返回會跳轉到上個頁面 (直接替換當前頁面)。
this.$router.go(n)
向前或者向后跳轉n個頁面,n可為正整數或負整數。
總結
到此這篇關于vue跳轉頁面常用的4種方法與區(qū)別的文章就介紹到這了,更多相關vue跳轉頁面方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!