3

I would like to get from which page a user came from in my page.

Currently, I would like to get this info in my created() or mounted() pages. Using this.$router or this.$route but none of them contain this info.

I know in the navigation guards, we can use this :

router.beforeEach((to, from, next) => { })

to know where the page is comming from, but I don't want to use router.beforeEach because that is in found inside my main.js but where I want to get the referer is in my components page, either in created/mounted methods

Is this possible?

hidar
  • 5,449
  • 15
  • 46
  • 70
  • You can either pass the router down as a prop, or use some kind of event system detailed here: https://vuejs.org/v2/guide/migration.html#dispatch-and-broadcast-replaced – Brian Glaz Jan 15 '18 at 15:12
  • what do you mean "pass the router down as a prop"? I could not find that on the doc – hidar Jan 15 '18 at 16:29
  • @hidar This is the document you are looking for. https://router.vuejs.org/en/essentials/passing-props.html – Onur Özkan Jan 16 '18 at 07:12

3 Answers3

2

You can use this.$route.query in mounted() hook in App.vue. You don't need router.beforeEach, because it dosn't make sense. App.vue mounted one time, when app is loaded, so this a good place to check router query params.

  • i trying this now and it works perfectly. my code in mounted: `console.log(this.$route.query);` and url `product/3?utm_source=2`. Console logs `{utm_source: "2"}` – Vladislav Gritsenko Jan 19 '18 at 13:38
  • hm.. sorry for the late response, I stumbled again on this issue and I still can not the referer. I have this code: `mounted () { console.log('this.$route.query', this.$route.query) }` – hidar Jan 22 '18 at 13:23
  • I don't know what else I can do to get the the referer. Do I need to pass a prop or something? – hidar Jan 22 '18 at 13:23
0

In

router.beforeEach((to, from, next) => { })

look for to.fullPath. It's your redirect link

sadeq shahmoradi
  • 1,395
  • 1
  • 6
  • 22
Yuriy
  • 1
-1
router.beforeEach((to, from, next) => {
       console.log(to.query)
       console.log(to.query.utm_source)
})

if url product/3?utm_source=2. Console logs

{utm_source: "2"}
2
Vicxx
  • 444
  • 4
  • 10