在根路径NuxtJS中使用get param

问题描述 投票:0回答:2

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

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

我试图用URL中的params访问它,但this.$route.params是空的。

nuxt.js
2个回答
2
投票

你在搅拌东西。 this.$route.params用于路线参数,例如_id.vue。例如,让我们说你有pages/some/_id.vue然后你加载url /some/param/和你的$route.params将是id:param

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

在这里查看docs


0
投票

JavaScript方式:

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

Can I use

© www.soinside.com 2019 - 2024. All rights reserved.