Is there any method you can use to check which parent your child was opened in?

39 views Asked by At

I am having an issue because I have a component that is used as a child component in two places, and I want to create a back button that sends the user back to the correct route. I can do it with the individual parents:

<button open ref="backBtn" :back="`/parent1/${this.$route.params.id}/home`" />
<button open ref="backBtn" :back="`/parent2/${this.$route.params.id}/home`" />

But what I really want is a single button with a method that says: Okay, you came from parent 1? Then use :back="/parent1/${this.$route.params.id}/home" else, use :back="/parent2/${this.$route.params.id}/home"

Can it be done?

2

There are 2 answers

0
ArrayConstructor On

You can just do this.$router.back() or $router.back(), it will go back from where it comes from.

If it doesn't work for you, you can use in your child the this.$parent to get information from your parent, even if it is not recommended.

0
jovan On

NOT recommended:

  1. router.go(-1)
  2. router.back() they work at the same way

it's better to pass navigation settings for each back button

const username = 'username'
...
<router-link :to="{
   router.push({ name: 'user', params: { username } })
}" />