How would I go on to fetch the id as parameter, when using params (not query) on router.push?
I assume my example is not optimal. How would I do it in a better way?
const Cities = Vue.component('city', {
template: '<h1>{{ }}</h1>',
created() {
fetch('')
.then((response) => response.json())
.then((result) => {
this.$router.push({ path: "/:id", params: { } });
this.cities = result
console.log(this.cities)
});
},
data() {
return {
cities: {
name: "",
id: ""
}
}
}
});
const router = new VueRouter({
routes: [
{
component: Cities,
path: '/:id'
}
]
});
new Vue({
el: '#app'
router
$router.pushyou can then later access those params inside the component withthis.$route.params.idfor example. I am not sure if you mean that, so just commenting for now.<div v-if="cities"><div v-for="city in cities">{{city.name}}</div></div>, also, what you define in the data function is a mismatch, it should be an empty array of cities instead of 1 object with id and name.