Home>

I am a beginner studying nuxt.js.
I would like to borrow the wisdom about the configuration file nuxt.config.js.

https://en.nuxtjs.org/examples/nested-routes
Is it possible to convert the users in env in the middle of nuxt.config.js in the "Nested Route Example" of this official website into a json file (like data.json below) and place it in the assets folder?
If i can tell me how to write if possible.

Thank you.

nuxt.config.js

export default {
  loading: false,
  head: {
    meta: [
      {charset: 'utf-8'},
      {name: 'viewport', content: 'width = device-width, initial-scale = 1'}
    ]
  },
  env: {
    users: [
      {id: 1, name: 'Kobe Bryant', number: 24},
      {id: 2, name: 'Michael Jordan', number: 23},
      {id: 3, name: 'Stephen Curry', number: 30},
      {id: 4, name: 'Lebron James', number: 23},
      {id: 5, name: 'Kevin Durant', number: 35},
      {id: 6, name: 'Kyrie Irving', number: 2}
    ]
  },
  generate: {
    routes: [
      '/ 1',
      '/ 2',
      '/ 3',
      '/Four',
      '/Five',
      '/ 6'
    ]
  }
}

data.json

[{
    "id": "1",
    "name": "Kobe Bryant",
    "number": "24"
  },
  {
    "id": "2",
    "name": "Michael Jordan",
    "number": "23"
  },
  {
    "id": "3",
    "name": "Stephen Curry",
    "number": "30"
  },
  {
    "id": "4",
    "name": "Lebron James",
    "number": "23"
  },
  {
    "id": "5",
    "name": "Kevin Durant",
    "number": "35"
  },
  {
    "id": "6",
    "name": "Kyrie Irving",
    "number": "2"
  }
]
  • Answer # 1

    It may be a misguided answer, but it seemed to simply parse the JSON file and set it to env.

    const fs = require ('fs');
    const users = JSON.parse (fs.readFileSync ('assets/data.json', 'utf8'));
    export default {
      loading: false,
      head: {
        meta: [
          {charset: 'utf-8'},
          {name: 'viewport', content: 'width = device-width, initial-scale = 1'}
        ]
      },
      env: {
        users: users
      },
      // ~~ omitted ~~
    }