Im a novice in NuxtJS. My page is structured with a navbar/menu, blog page with all articles listed and a couple of mostly static pages.(like most company websites)
I am retrieving my data from a Strapi API, where I can only fetch single entries by their ID.
What I have to do:
When the user clicks on the navbar link, I need to pass the ID of the corresponding article/post to the article component in order to retrieve the article from the API. So far it is working quite well, but doing it via router params, an article will have an URL like https://www.example.com/posts/63ndjk736rmndhjsnk736r
What I would like to do:
I would like to have an URL with a slug https://www.example.com/posts/my-first-Post
and still pass the ID to the article component/page.
Whats the best way to do that?
2
Answers
You can use like
_slug.vue
in pages folder.and your routes like this;
then create a new vue file.
this way you can also access the parameter
You can use
query
. Here is some example:I have some data:
In my navbar list:
How your routes show:
What you need:
query
namedid
which is your single entry IDconst postId = this.$route.query.id