【问题标题】:Select a layout for dynamically generated nuxt page为动态生成的 nuxt 页面选择布局
【发布时间】:2018-11-19 16:47:07
【问题描述】:

我正在构建一个项目,其中我的所有数据(包括页面路由)都来自 GraphQL 端点,但需要通过静态站点托管(我知道,我知道。不要让我开始)。

我已经设法使用nuxt.config.js 中的以下代码从数据中静态生成路由:

 generate: {
    routes: () => {
      const uri = 'http://localhost:4000/graphql'
      const apolloFetch = createApolloFetch({ uri })
      const query = `
          query Pages {
           pages {
            slug
            template
            pageContent {
             replaced
             components {
              componentName
              classes
             body
            }
          }
        }
      }
      `

      return apolloFetch({ query }) // all apolloFetch arguments are optional
        .then(result => {
          const { data } = result
          return data.pages.map(page => page.slug)
        })
        .catch(error => {
          console.log('got error')
          console.log(error)
        })
    }
  }

我要解决的问题是某些页面需要使用与默认布局不同的布局,要使用的正确布局在 GraphQL 数据中指定为 page.template 但我看不到任何方法可以通过它路由器的信息。

我尝试将return data.pages.map(page => page.slug) 更改为:

  return data.pages.map(page => {
     route: page.slug,
     layout: page.template
    })

但这似乎是一个非首发。有谁知道如何将布局首选项传递给 vue 路由器?

【问题讨论】:

标签: vue.js vue-router nuxt.js


【解决方案1】:

一种方法是将数据注入有效负载 https://nuxtjs.org/api/configuration-generate#speeding-up-dynamic-route-generation-with-code-payload-code- 这将允许您将生成信息传递到路由本身。

【讨论】:

  • 这就是我最终做的 :) 然后我说“去他妈的”,当下一个问题出现时,我就切换到了 Gatsby。 :)
  • 你考虑过Gridsome吗?
猜你喜欢
  • 2020-07-04
  • 2020-07-29
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-18
相关资源
最近更新 更多