问题
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?
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/48265620/how-to-get-http-refere-in-vuejs