【发布时间】:2020-07-21 18:32:21
【问题描述】:
应该如何构建文件和文件夹,以便它们使用 URL 查询字符串参数而不是常规参数?
例子:
网址参数:
文件夹结构:
pages/
---|comments/
------|_id.vue
这种结构导致这条路线:
pages/comments/page/1
需要如何构建文件夹/文件,以便我的路线如下所示?
网址查询字符串参数:
pages/comments?page=1
【问题讨论】:
-
我认为 url 查询不是路由的一部分,因此您只需使用
/pages/comments路由,然后稍后在组件中访问查询,例如this.$router.query.page。见相关nuxtjs.org/guide/routing#locally-accessing-route-params。 -
在 created() 钩子上使用 this.$router.query.page 设置页面时,它不会显示在浏览器的 URL 中,而是附加到 $router 对象。
-
对于导航,您应该使用
<router-link :to='/pages/comments?page=1'>或<router-link :to='{ name: 'commentName', query: { page: 1 } }'>或this.$router.push('/pages/comments?page=1')或 ... -
是的,但我正在尝试设置初始页面加载参数。我使用
this.$router.push({path: this.$route.path,query: { page: this.currentPage }})取得了一些成功
标签: vue.js vuejs2 nuxt.js vue-router