【问题标题】:Error creating custom pages from local plugin within GatsbyJS在 GatsbyJS 中从本地插件创建自定义页面时出错
【发布时间】:2018-12-16 10:54:50
【问题描述】:

我的目标是从本地插件创建页面。我编写了一个名为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`,
            }
        },
    ]
}

plugins/my-custom-plugin/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'
        }
    })
}

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

问候。

【问题讨论】:

    标签: javascript reactjs gatsby


    【解决方案1】:

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

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

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

    【讨论】:

    • 我当然知道。但我的目的是清理根项目中的gatsby-node.js
    • 仍然不需要您的本地插件。你可以像 React 的文档网站那样做:github.com/reactjs/reactjs.org/blob/…
    猜你喜欢
    • 1970-01-01
    • 2018-08-31
    • 2013-07-24
    • 1970-01-01
    • 2015-07-25
    • 2014-02-22
    • 2018-11-20
    • 1970-01-01
    • 2014-11-10
    相关资源
    最近更新 更多