Search code examples
javascriptvue.jsvue-router

vue-router – check if child of specific subroute


Checked similar questions on stackoverflow but couldn't find a working solution.

I want to check if a subroute is the child of a specific route to display a container (or not).

The following doesn't work unfortunately:

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

I'm trying to display the div container on www.example.com/projects AND on www.example.com/projects/foo

I also tried omitting the this.

Thanks for any tips!


Solution

  • 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