简体   繁体   English

在nuxt配置文件中访问存储

[英]Access store in nuxt config file

I want to add dynamic routes to be statically generated via Nuxt. 我想添加动态路由以通过Nuxt静态生成。

I have a client/server-side store defined thanks to asyncData method. 由于asyncData方法,我定义了客户端/服务器端存储。 I want to map this store value (an array) in my nuxt.config.js file to make it "dynamic/static" routes map for nuxt generate command - but how to access store in that file? 我想在我的nuxt.config.js文件中映射这个商店值(数组),使其成为nuxt generate命令的“动态/静态”路由映射 - 但是如何访问该文件中的存储? is this even possible? 这有可能吗?

You cannot access the store from the nuxt.config.js file. 您无法从nuxt.config.js文件访问该存储。
The nuxt.config.js file is just a global configuration. nuxt.config.js文件只是一个全局配置。

However, you can set dynamic routes for the generate commmand. 但是,您可以为生成命令设置动态路由。
If you want nuxt.js to generate routes with dynamic params, you need to set an array of dynamic routes. 如果希望nuxt.js生成具有动态参数的路径,则需要设置动态路径数组。

From the official documentation : 官方文档

nuxt.config.js nuxt.config.js

module.exports = {
  generate: {
    routes: [
      '/users/1',
      '/users/2',
      '/users/3'
    ]
  }
}

If you have dynamic parameters in your routes you have two options: 如果路线中有动态参数,则有两种选择:

1) A Function which returns a Promise 1)返回Promise的函数
2) A Function with a callback(err, params) 2)带回调的函数(错误,参数)

Please see the documentation for more details on this: https://nuxtjs.org/api/configuration-generate/#routes 有关详细信息,请参阅文档: https//nuxtjs.org/api/configuration-generate/#routes

Posting for future reference – 张贴供将来参考 -

One solution I found was to just hardcode the data you want into the .config file. 我发现的一个解决方案是将您想要的数据硬编码到.config文件中。

Practical for my situation, but not always scalable. 对我的情况很实用,但并不总是可扩展的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM