[Vuejs]-How to route to the route's children path in Vue

0👍

So you want to add a query param on a specific route, if it is not set?

You can also do it like this in your place-ad page component:

<script>
export default {
  created () {
    if (!this.$route.query.ad_id) {
      this.$router.push({
        query: {
          ad_id: '12345',
          ...this.$route.query
        }
      })
    }
  }
}
</script>

Or, if you have to stick to the beforeRouteLeave, this one should work too:

  beforeRouteLeave (to, from, next) {
    if (to.path === '/place-ad') {
      if (!to.query.ad_id) {
        return next({
          path: to.path,
          query: {
            ...to.query,
            ad_id: '12345'
          }
        })
      }
    }

    return next()
  }

Leave a comment