Vue系列:通過vue-router如何傳遞參數


使用vue-router 來實現webapp的頁面跳轉,有時候需要傳遞參數,做法如下:

主要有以下幾個步驟:
(1) 設置好路由配置
router.map({
  '/history/:deviceId/:dataId': {
    name: 'history', // give the route a name
    component: { ... }
  }
})
這里有2個關鍵點:
    a)給該路由命名,也就是上文中的 name: 'history',
    b)在路徑中要使用在路徑中使用冒號開頭的數字來接受參數,也就是上文中的 :deviceId, :dataId;
 
(2)在v-link中傳遞參數;
    <a v-link="{ name: 'history', params: { deviceId: 123, dataId:456 }}">history</a>
這里的123,456都可以改用變量。
比如該template所對應的組件有2個變量定義如下:
data: function() {
    return {
      deviceId:123,
       dataId:456
          }
}    
此時上面那個v-link可以改寫為:
    <a v-link="{ name: 'history', params: { deviceId: deviceId, dataId: dataId }}">history</a>
(3)在router的目標組件上獲取入參
    比如在router目標組件的ready函數中可以這么使用。
    ready: function(){
         console.log('deviceid: ' + this.$route.params.deviceId);
          console.log('dataId: ' + this.$route.params.dataId);
    }


————完————






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM