首页 > 解决方案 > 在 GatsbyJS 中从本地插件创建自定义页面时出错

问题描述

我的目标是从本地插件创建页面。我写了一个名为my-custom-plugin. 我还安装了gatsby-plugin-page-creator插件来自动从默认pages目录之外的组件创建页面。

这是我的项目结构:

plugins
    /my-custom-plugin
        /gatsby-node.js
        /package.json
src
    /components
        /pages
            /single.js
gatsby-config.js
gatsby-node.js
...etc

gatsby-config.js(从根目录):

module.exports = {
    plugins: [
        `my-custom-plugin`,
        {
            resolve: `gatsby-plugin-page-creator`,
            options: {
                path: `${__dirname}/src/components/pages`,
            }
        },
    ]
}

插件/我的自定义插件/gatsby-node.js

const path = require('path')
const location = path.resolve(__dirname, '..', '..', '/src/components/pages')
exports.createPages = ({ actions }) => {
    const { createPage } = actions
    createPage({
        path: `/sample-page`,
        component: `${location}/single.js`,
        context: {
            slug: 'sample-page'
        }
    })
}

不幸的是,我The plugin "my-custom-plugin" created a page with a component that doesn't exist在运行 gatsby develop 时收到错误消息。我做错了吗?有什么帮助吗?

问候。

标签: javascriptreactjsgatsby

解决方案


https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-page-creator

您不需要自定义插件。自述文件指出您只需将配置插入gatsby-config.js.

您当前的本地插件尝试完全按照页面创建器插件所做的工作。


推荐阅读