[Vuejs]-Vue.js accessing this.$route.params in mounted functions

0๐Ÿ‘

If you want a more elegant solution for parsing the router param id check tis out:

index.js(router file)

{
    path: '/directors/:id',
    name: 'Directors',
    component: myComponentName,
    props: (route) => {
      const id = Number.parseInt(route.params.id);
      return { id }
    }
  }

Component.vue

 props: {
    id: {
      required: true,
      type: Number,
    }

With the above implementation you can remove the parsing in the component and also instead of doing this:

this.$route.params.id;

Now you can do:

this.id

And you have the parsed id with best practises ๐Ÿ˜‰

Cheers

Leave a comment