How to get http refere in vuejs?

元气小坏坏 提交于 2021-01-27 18:24:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!