A
A
Anton Shvets2018-03-22 13:55:23
Vue.js
Anton Shvets, 2018-03-22 13:55:23

How to generate pages in Nuxt based on api response results?

Has a folder structure

pages
|  index.vue
| posts
|  |  index.vue
|  |  _post.vue

in posts/index.vue I get an array from the server with a list of posts, I render the "table of contents", the post is requested on click, rendered and displayed.
After nuxt generatethat, I get one index.html in the posts folder
. As a result, only the request to /posts works, the request to /posts/postname, of course, leads to 404. You can redirect to the index, but this is no longer SSR.
Is it possible to tell nuxt to render pages from a given list?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2018-03-22
@Xuxicheta

I myself will answernuxt.config.js

generate: {
    routes: async function () {
      /* получить список постов, у которых есть свойство postname */
      const { data: posts }  = await axios.get('https://mydomain/posts'); 
      /* вернуть массив адресов вида '/posts/%postname%' */
      return posts.map(u => `/posts/${u.postname}`);
    }
  },

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question