VUE路由跳轉(zhuǎn)頁面?zhèn)鲄⒌姆绞剑?br>
?方式一:聲明式導(dǎo)航(router-link)
1、不帶參數(shù)? (注意:鏈接中如果是“/”開始就是從根路由開始,否則就是從當(dāng)前路由開始)
<router-link? :to="{name:home}" />? ? ? ? ? ?
?<router-link? :to="{path:/home}" />
2、帶參數(shù)
<router-link? :to="{name:‘home’, params: {id:1}}" />
// 路由配置? path:"/home:/id" 或? path:"/home:id"
// 不配置path,第一次可請求,刷新后id會消失,若配置path刷新頁面id會保留
?方式二:編程式導(dǎo)航? this.$router.push()
1、不帶參數(shù)
this.$router.push("/home")?
this.$router.push("{name:'home'}")
this.$router.push("{path:'/home'}")
2、帶參數(shù)(注意:此處分為query傳參和params傳參并注意兩者的區(qū)別)
this.$router.push("{name:'home',params:{id:1}}")
this.$router.push("{path:'/home',query:{id:1}}")
// html獲取參數(shù)$route.params.id, script獲取參數(shù)this.$route.params.id
// 注意name和params搭配,path和query搭配
?兩者的區(qū)別:
? params類似于post屬于密文傳參,參數(shù)不會顯示到URL地址欄中,如有涉及到隱私的用其較合適,? ? ? ? ? ? ? ? ? ? ? ? ?
? query類似于get,所傳的參數(shù)會顯示到URL地址欄中,相對來說安全性不高? ? ? ? ? ? ? ? ? ? ? ? ?
? ?params傳參刷新會無效,但是query會保存?zhèn)鬟f過來的值,刷新不變
3、this.$router.replace()? 用法參考push的用法
4、this.$router.go(n)()? ?此方法是向前或向后跳轉(zhuǎn)n個頁面,n為正整數(shù)或者負(fù)整數(shù)
注意其三者之間的區(qū)別:
this.$router.push()是跳轉(zhuǎn)到指定url路徑,并在history中添加一個記錄,點擊返回會跳到上一個頁面? ? ? ?
this.$router.replace()是跳轉(zhuǎn)到指定的url路徑,但是在history中沒有記錄,點擊返回會跳到上上個頁面?? ? ? ?
?this.$router.go(n)()是向前或向后跳轉(zhuǎn)n個頁面,n為正整數(shù)或者負(fù)整數(shù)
總結(jié):
關(guān)于路由跳轉(zhuǎn)頁面的方法博主介紹到這里,希望對大家有所幫助。如果大家有任何疑問請給我留言,博主看到后會及時回復(fù)。