主要有以下幾個步驟:
(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);
}
————完————
