Answer the question
In order to leave comments, you need to log in
How to pass URL parameters to Vue.js?
I am making an application with Laravel and Vue.js. I was wondering how to pass a url parameter like user id or user slug to use this to display user's posts on his personal page.
For example, if you click on the link project.com/posts/vasya, then all the posts of this user are displayed.
Now the code looks like this:
const app = new Vue({
el: '#app',
data: {
posts: [],
},
created(){
axios.get('project.com/posts/{slug}')
.then(response => {
console.log(response);
this.posts = response.data;
})
.catch(function (error) {
console.log(error);
});
},
Answer the question
In order to leave comments, you need to log in
Use vue-router
https://router.vuejs.org/en/essentials/dynamic-mat...
Route example
const router = new VueRouter({
routes: [
{ path: '/post/:slug', component: User }
]
})
watch: {
'$route'(to) {
axios.get(`project.com/posts/${to.params.slug}`).then();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question