To retrieve the hotelID
, you can access it through $route.params
. Refer to the documentation for more details. Once you have the ID, you can find the corresponding hotel data in Hotels.json
using $route.params.id
. Check out the Demo
Hotels.json
{
"hotels": [
{
"id": 1,
"name": "hotel 1"
},
{
"id": 2,
"name": "hotel 2"
},
{
"id": 3,
"name": "hotel 3"
}
]
}
router.js
const routes = [
{
path: "/",
name: "hotels",
component: function () {
return import("@/components/Hotels.vue");
}
},
{
path: "/hotel/:id",
name: "hotel",
component: function () {
return import("@/views/Hotel.vue");
}
}
];
App.vue
<template>
<div id="app">
<div id="nav">
<router-link to="/">Hotels</router-link>|
<template v-for="(item, key) in hotels">
<router-link :key="key" :to="`/hotel/${hotels[key].id}`">{{
hotels[key].name
}}</router-link
>|
</template>
</div>
<router-view />
</div>
</template>
<script>
import json from "./Hotels.json";
export default {
name: "App",
data() {
return {
hotels: json.hotels,
};
},
};
</script>
Hotel.vue
<template>
<div>
<h1>{{ hotel.name }}</h1>
</div>
</template>
<script>
import json from "../Hotels.json";
export default {
data() {
return {
hotels: json.hotels,
};
},
computed: {
hotel() {
return this.hotels.find((item) => item.id === +this.$route.params.id);
},
},
};
</script>