【问题标题】:Use get param in root route NuxtJS在根路由 NuxtJS 中使用 get param
【发布时间】:2019-04-15 19:38:31
【问题描述】:

我正在尝试在根路由中获取GET 参数。 我从文档中读到我需要_id.vue 文件,并且我将它与“index.vue”处于同一级别。在index.vue我有:

beforeMount () {
  console.log(this.$route);
}

我正在尝试使用 URL 中的参数访问它,但 this.$route.params 为空。

【问题讨论】:

    标签: nuxt.js


    【解决方案1】:

    你在混合东西。 this.$route.params 用于路由参数,例如_id.vue。例如,假设您有pages/some/_id.vue,然后您加载网址/some/param/,而您的$route.params 将是id:param

    但如果你想获得GET 参数,例如/some?param=value 你不需要_id。它刚刚通过this.$route.query访问

    在此处查看docs

    【讨论】:

      【解决方案2】:

      JavaScript方式:

      const urlParams = new URLSearchParams(window.location.search);
      const id = urlParams.get('id'); // ?id=123
      

      Can I use

      【讨论】:

        猜你喜欢
        • 2018-10-01
        • 1970-01-01
        • 2016-06-14
        • 2019-01-15
        • 2021-11-22
        • 2020-09-07
        • 2021-09-17
        • 2019-02-12
        • 2019-07-20
        相关资源
        最近更新 更多