简体   繁体   English

Nuxt 基于 .json 文件数据生成动态页面

[英]Nuxt generate dynamic pages based on .json file data

I am having a fairly easy problem.我有一个相当简单的问题。

In my nuxt app I want to display a list of items based on objects in an array in a simple .json file.在我的 nuxt 应用程序中,我想在一个简单的 .json 文件中显示基于数组中对象的项目列表。 Furthermore when the specific item is clicked I want to show a detailed view of that item with more data.此外,当单击特定项目时,我想显示该项目的详细视图以及更多数据。 Imagine it is a list about cars, a dynamic route would look like: "cars/ford-500".想象一下这是一个关于汽车的列表,一个动态路线看起来像:“cars/ford-500”。

Now if I populate a vuex variable and display that in the dynamic route component - I dont get the SEO benefits since its loaded by the client.现在,如果我填充一个 vuex 变量并将其显示在动态路由组件中 - 我没有得到 SEO 的好处,因为它是由客户端加载的。 Furthermore if I want to refresh the page with the dynamic link I get an error because the store is deleted after refresh.此外,如果我想用动态链接刷新页面,我会收到一个错误,因为刷新后商店被删除了。 Another option is passing a route parameter but then again on refresh I get the error.另一种选择是传递路由参数,但在刷新时再次出现错误。

Basically I dont want to create 100 html pages I just want to let nuxt-generate do it automatically with my .json file and I want to display the specific data for that list item when I reload the dynamic route.基本上我不想创建 100 个 html 页面,我只想让 nuxt-generate 使用我的 .json 文件自动完成,并且我想在重新加载动态路由时显示该列表项的特定数据。 I know nuxt-generate does not look at dynamic paths but there is a nuxt-generate property in the config file one can use to tell them to loop through.我知道 nuxt-generate 不查看动态路径,但是配置文件中有一个 nuxt-generate 属性,可以用来告诉它们循环。

Thank you so much for your help!非常感谢你的帮助!

If you want Nuxt.js to generate routes with dynamic params, you need to set the generate.routes property to an array of dynamic routes.如果你想让 Nuxt.js 生成带有动态参数的路由,你需要将 generate.routes 属性设置为一个动态路由数组。

If you have a list with the urls you can do it like so:如果你有一个包含 url 的列表,你可以这样做:

 // nuxt.config.js

 generate {
    routes () {
      // You can also import this from a js file.
      const yourUrls = ['ford-500', 'porche-spyder', 'mclaren-senna', 'volkswagen-beetle'];
      const routesToGenerate = [];

      for (const url of yourUrls) {
        const newRoute = '/cars/' + url;
        routesToGenerate.push(newRoute);
      }

      return routesToGenerate;
    }
}

Of course you can use a .json file as well当然你也可以使用 .json 文件

 // nuxt.config.js

 generate {
    routes () {
      const routesToGenerate = [];

      for (const urlKey of Object.keys(yourJsonFile)) {
        const newRoute = '/cars/' + yourJsonFile[urlKey];
        routesToGenerate.push(newRoute);
      }

      return routesToGenerate;
    }
}

If you need to you can also pass payload into the component you're generating.如果需要,您还可以将有效负载传递到您正在生成的组件中。 You can read more about it in the documentation here.您可以在此处的文档中阅读更多相关信息。 ( https://nuxtjs.org/api/configuration-generate/#routes ) ( https://nuxtjs.org/api/configuration-generate/#routes )

Nuxt-i18n Nuxt-i18n

If you are using an internationalization module like nuxt-i18n you'll need to manually define the locale prefix如果您使用的是像 nuxt-i18n 这样的国际化模块,则需要手动定义语言环境前缀

 // nuxt.config.js

 generate {
    routes () {
      // You can also import this from a js file.
      const yourUrls = ['ford-500', 'porche-spyder', 'mclaren-senna', 'volkswagen-beetle'];
      const yourLocales = ['da-DK', 'en', 'de']
      const routesToGenerate = [];

      for (const url of yourUrls) {
        const defaultRoute = '/cars/' + url;
        routesToGenerate.push(defaultRoute);
            for (const locale of yourLocales) {
                const localeRoute = locale + '/cars/' + url;
                routesToGenerate.push(localeRoute);
            }
      }

      return routesToGenerate;
    }
}

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

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