VUE學(xué)習(xí)筆記-頁面跳轉(zhuǎn)

在最近的項目開發(fā)中,需要使用到Vue的頁面跳轉(zhuǎn),就記錄一下常用的幾種:

原生JS

var url = 'https://10.50.1.10:8888/'
window.open(url)//打開新頁面
window.location.href = url//跳轉(zhuǎn)頁面

template

  <router-link to="/recommend">
      <button class="button">點擊跳轉(zhuǎn)</button>
 </router-link>

$router.push

有時候我們需要的是點擊按鈕跳出彈窗,選擇判斷后進(jìn)行跳轉(zhuǎn),我們常用.$router.push 跳轉(zhuǎn) 傳參:

<button @click = "func()">跳轉(zhuǎn)</button>

<script>
    export default{
        methods:{
            func (){
                this.$router.push({name: '/order/page1',params:{ id:'1'}});
            }
        }
    }
</script>

另有:

this.$router.push({path: ''/order/index''});
this.$router.push({path: '/order/page1',query:{ id:'2'}});
this.$router.push({name: '/order/page2',params:{ id:'6'}});

//  path:'路由里配置的index.vue的路徑'
//  params:{id:'1',name:'eva'} /query:{id:'5'}  需要傳遞的參數(shù)和值


路由傳參params 和query兩種方式的區(qū)別:

用法上的

剛才已經(jīng)說了,query要用path來引入,params要用name來引入,接收參數(shù)都是類似的,分別是this.route.query.namethis.route.params.name。

注意接收參數(shù)的時候,已經(jīng)是route而不是router了哦!!

展示上的

query更加類似于我們ajax中g(shù)et傳參,params則類似于post,說的再簡單一點,前者在瀏覽器地址欄中顯示參數(shù),后者則不顯示。

三、路由參數(shù)的取值:

{{this.$route.params.參數(shù)名}}
//注意:接收參數(shù)的時候已經(jīng)是 route 而不是router了
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容