Reputation: 27
I need to change the data in a Vue Component depending on the route. For example, when the page Aircraft is loaded I want the data to contain the Aircraft images. If the page Animals is then loaded then I want the data to contain the Animals images.
The website can be seen here: https://test.joebaileyphotography.com/Portfolio
Vue.component('subitem', {
props: ['photo'],
template: `
<a :href="photo.img">
<img :src="photo.img" width="100%" height="auto">
</a>
`
});
const subitem = {
data() {
return {
photos: [
{img: 'Images/Portfolio/Aircraft/image1.jpg'},
{img: 'Images/Portfolio/Aircraft/image1.jpg'},
{img: 'Images/Portfolio/Aircraft/image1.jpg'}
],
}
},
template: `
<div>
<breadcrumbs></breadcrumbs>
<info></info>
<div class="portfolio" id="portfolio" data-featherlight-gallery data-featherlight-filter="a:not(.all)">
<subitem
v-for="subitem in photos"
:key="subitem.photo"
v-bind:photo="subitem">
</subitem>
</div>
</div>
`
};
const router = new VueRouter({
mode: 'history',
routes: [
{ path: '/Portfolio/:id', component: subitem },
{ path: '/Portfolio', component: item }
]
});
Upvotes: 0
Views: 2054
Reputation: 37753
You can use Vue-router's feature Dynamic Route Matching and access the :id
using this.$route.params.id
in your component
Or you can pass route parameters as props.
Just change your route definition to { path: '/Portfolio/:id', component: subitem, props: true },
and define id
prop in your subitem
component
You can use passed id
to filter the array of your photos, for example like this:
computed: {
getPhotosById(id) {
return this.photos.filter(photo => photo.igm.startsWith(`Images/Portfolio/${id}/`))
}
}
and in the template do v-for="photos in getPhotosById($route.params.id)"
Upvotes: 1