【发布时间】:2019-04-15 19:38:31
【问题描述】:
我正在尝试在根路由中获取GET 参数。
我从文档中读到我需要_id.vue 文件,并且我将它与“index.vue”处于同一级别。在index.vue我有:
beforeMount () {
console.log(this.$route);
}
我正在尝试使用 URL 中的参数访问它,但 this.$route.params 为空。
【问题讨论】:
标签: nuxt.js
我正在尝试在根路由中获取GET 参数。
我从文档中读到我需要_id.vue 文件,并且我将它与“index.vue”处于同一级别。在index.vue我有:
beforeMount () {
console.log(this.$route);
}
我正在尝试使用 URL 中的参数访问它,但 this.$route.params 为空。
【问题讨论】:
标签: nuxt.js
你在混合东西。 this.$route.params 用于路由参数,例如_id.vue。例如,假设您有pages/some/_id.vue,然后您加载网址/some/param/,而您的$route.params 将是id:param。
但如果你想获得GET 参数,例如/some?param=value 你不需要_id。它刚刚通过this.$route.query访问
在此处查看docs
【讨论】:
JavaScript方式:
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id'); // ?id=123
【讨论】: