Gatsby入門指南—添加博客內容頁(4)

1.查數據

注意,這裏跟前面不一樣了,我用gatsby-node.js這個文件去提供數據,沒有什麼爲什麼,規定,照做就好。

const path = require("path");
exports.createPages = ({ actions, graphql }) => {
  const { createPage } = actions
​
  const blogPostTemplate = path.resolve(`src/templates/blogPost.js`)
​
  return graphql(`
    {
      allMarkdownRemark{
        edges {
          node {
            frontmatter {
              path,
              title,
              tags
            }
          }
        }
      }
    }
  `).then(result => {
    if (result.errors) {
      return Promise.reject(result.errors)
    }
    const posts = result.data.allMarkdownRemark.edges;
    createTagPages(createPage, posts);
    posts.forEach(({ node }, index) => {
      const path = node.frontmatter.path;
      const title = node.frontmatter.title;
      createPage({
        title,
        path,
        component: blogPostTemplate,
        context: {
          pathSlug: path
        
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章