2020-09-02 VueRouter 編程式導(dǎo)航

編程式導(dǎo)航

注意:在 Vue 實(shí)例內(nèi)部,你可以通過 router 訪問路由實(shí)例。因此你可以調(diào)用 this.router.push

    1. 聲明式導(dǎo)航
      <router-link></router-link>
    1. 編程式導(dǎo)航
      js 路由對(duì)象提供的 方法來 跳轉(zhuǎn),路由實(shí)例調(diào)用方法(router.push)router 路由 給Vue灌入一個(gè)對(duì)象,就是 路由對(duì)象(new VueRouter())
**使用$router.push()方法**
1. 參數(shù)為字符串
this.$router.push('跳轉(zhuǎn)路由的path')

2. 參數(shù)為對(duì)象
this.$router.push({
    path:'/home'
})

this.$router.push({
    name:'home' //命名路由
})

編程式導(dǎo)航路由傳參匯總

    1. 動(dòng)態(tài)路由傳參
{
      // 動(dòng)態(tài)路徑參數(shù) 以冒號(hào)開頭
      path:'/news/:id',
      component: NewsSon
},
    1. query傳參 (最好搭配path)
this.$router.push({
      //query傳參 path一起使用 (最好不要和name一起使用)
          /*
     優(yōu)點(diǎn):
           頁面刷新 參數(shù)不丟失
      缺點(diǎn):
          參數(shù) 放在url上 顯式的
           獲取:
             this.$route.query.a
           this.$route.query.b
         */
          path:'/home',
          query:{
           a:10,
          b:20
     }
  })
    1. params傳參(搭配name命名路由)
this.$router.push({
          //params傳參 (和name是固定搭配)
          /*
            優(yōu)點(diǎn):
              隱式 (地址上不顯示)
            缺點(diǎn):
              刷新丟失
            注意:
              params 傳參和命名固定搭配
            獲取
              this.$route.params.a
          */
          name:'news',
          params:{
            a:10,
            b:30
          }
        })

編程式導(dǎo)航 go

this.$router.go(n)
  n
    0  刷新
    -1 回退  
    1 前進(jìn)一步

路由模式

 路由模式
         hash路由   默認(rèn)的
            window.addEventListener("hashchange",(e)=>{
              // 根據(jù)hashchange 事件 來動(dòng)態(tài)切換 不同組件
            })
         history
            路由好處:不再有#存在
            原理:
              history對(duì)象有一個(gè) pushState
          注意:
          此模式 需要 服務(wù)器 做配置
          /
          服務(wù)配置:
            如果 你的地址 / 重定向到 當(dāng)前 index.html(有vue,vueRouter)下
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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