[Vuejs]-Vue-router โ€“ check if child of specific subroute

2๐Ÿ‘

โœ…

You can use

<div v-if="this.$route.matched.some(route => route.path.includes('/projects'))">
   etc.
</div>

includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate so www.example.com/projects/foo and www.example.com/projects return true

๐Ÿ‘คctm

1๐Ÿ‘

If I need to hide some div in one of the paths I am using this.$route.path property.

<div v-if="this.$route.path === '/projects'">
  Display only on views witch route is /projects
</div>

If you want to hide div in all paths which contains โ€˜/projectsโ€™ you can make it in this way.

<div v-if="this.$route.path.indexOf('/projects') >= 0">
  Display only on views which contains /projects
</div>
๐Ÿ‘คlaurisstepanovs

1๐Ÿ‘

The best thing is to check on the name of the route

['route1', 'subroute1'].indexOf($route.name) >= 0
๐Ÿ‘คybenhssaien

Leave a comment