【问题标题】:How to get http refere in vuejs?如何在 vuejs 中获取 http referer?
【发布时间】:2018-01-15 15:06:47
【问题描述】:

我想在我的页面中获取用户来自哪个页面。

目前,我想在我的 created() 或 mount() 页面中获取此信息。使用 this.$router 或 this.$route 但它们都不包含此信息。

我知道在导航守卫中,我们可以使用这个:

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

要知道页面来自哪里,但我不想使用 router.beforeEach 因为它位于我的 main.js 中,但我想要获取引用者的位置在我的组件页面中,或者在创建/挂载的方法

这可能吗?

【问题讨论】:

标签: vue.js vuejs2 vue-router


【解决方案1】:

您可以在 App.vue 的mounted() 挂钩中使用this.$route.query。您不需要 router.beforeEach,因为它没有意义。 App.vue 在加载应用时安装一次,因此这是检查路由器查询参数的好地方。

【讨论】:

  • 我现在试试这个,它工作得很好。我的代码已安装:console.log(this.$route.query); 和 url product/3?utm_source=2。控制台日志{utm_source: "2"}
  • hm.. 很抱歉回复晚了,我在这个问题上再次绊倒了,我仍然不能推荐人。我有这个代码:mounted () { console.log('this.$route.query', this.$route.query) }
  • 我不知道我还能做些什么来获得推荐人。我需要传递道具或其他东西吗?
【解决方案2】:
router.beforeEach((to, from, next) => {
       console.log(to.query)
       console.log(to.query.utm_source)
})

如果网址product/3?utm_source=2. 控制台日志

{utm_source: "2"}
2

【讨论】:

    猜你喜欢
    • 2018-01-24
    • 2014-12-30
    • 2014-06-08
    • 2012-10-12
    • 2018-05-04
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 2011-09-17
    相关资源
    最近更新 更多