Nuxt.JS: How to get route url params in a page

后端 未结 8 798
深忆病人
深忆病人 2021-02-06 21:06

I am using Nuxt.js, and have a dymanic page which is defined under

pages/post/_slug.vue

So, when I visit the page url, say, http://localhost:3

相关标签:
8条回答
  • 2021-02-06 21:35

    Simply you can access routing parameters

    for global uses but it is not good practice:

    window.$nuxt._route.params

    for local uses under pages/components/layout etc, Always we should practice like below

    this.$route

    or

    this.$nuxt._route.params

    0 讨论(0)
  • 2021-02-06 21:36

    To the best of my knowledge, this already is the best way, if not the only one to do that. But I could suggest an slightly different approach that maybe fit to your needs. Use the asyncData method to retrieve the data from the server instead of put a param at your VM and process later, if is your case. Then, you can handle the result data at the presentation logic and not any kind of request. On the other hand, also you could use, fetch if you don't want to pass anything to the VM or use a middleware instead, according to your needs.

    0 讨论(0)
提交回复
热议问题