如何在Sanity模式中导出和导入一个导出默认文件的目录?
如何在Sanity模式中导出和导入一个导出默认文件的目录?
大部分我阅读的教程和文档都使用了以下结构来编写 Sanity 的 schema.js 文件:
import createSchema from 'part:@sanity/base/schema-creator' import schemaTypes from 'all:part:@sanity/base/schema-type' import blog from './documents/blog' import author from './documents/author' export default createSchema({ name: 'default', types: schemaTypes.concat([ blog, author, ]), })
但我想将整个目录作为一个单独的源文件导入,我尝试了以下方法:
schema.js:
import createSchema from 'part:@sanity/base/schema-creator' import schemaTypes from 'all:part:@sanity/base/schema-type' import * as documents from './documents/' export default createSchema({ name: 'default', types: schemaTypes.concat([ documents, ]), })
/documents/index.js:
export { default as author } from './author' export { default as blog } from './blog'
我还尝试了以下方法:
import author from './author' import blog from './blog' export { author, blog }
在 schema.js 中将所有单独的文件引入可以正常工作,但当我尝试更改文件结构以引用一个文件时,会出现以下错误:
来自 http://localhost:3333/desk/
。
研究
- Problem while exporting a function from another file in React
- What does "export default" do in JSX?
- Why es6 react component works only with "export default"?
- does not contain a default export even after being correctly imported in React
- ES6 exporting/importing in index file
- React - cannot export multiple files from index.js
- Export all files in
index.ts
在 Sanity 中,我如何将一个目录中的所有文件导出,并在 Sanity 的 schema 中作为一行导入?
问题的原因:导出和导入Sanity模式中的默认文件夹目录时出现问题。
解决方法:根据给出的代码,可以尝试以下方法进行解决:
1. 在`/documents/index.js`文件中,将`export { default as blog } from './blog'`改为`export { default as blog } from './blog'`。
2. 在`/schema.js`文件中,导入`createSchema`模块和`schemaTypes`模块,并导入`documents`文件夹的所有内容。然后使用`Object.values()`方法获取所有`documents`文件夹的值,并将其与`schemaTypes`合并为一个新的数组。最后,使用`createSchema()`方法创建一个新的模式并将其导出。
注意:上述代码中的`...allDocuments`表示将`allDocuments`数组中的所有元素展开为单独的元素。
完成以上步骤后,应该能够成功导出和导入Sanity模式中的默认文件夹目录。
希望以上解决方案对您有帮助!