I
I
iAlex1952018-05-07 17:01:24
Vue.js
iAlex195, 2018-05-07 17:01:24

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);
  			});

    },

I found information about vue-router, but it's not clear yet how to use it in this case.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Shahob I, 2018-05-09
@shahob

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();
       }
}

H
hopeful_romantic, 2018-05-07
@hopeful_romantic

axios.get('project.com/posts/' + slug)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question