【问题标题】:Typescript types for wordpress Gutenberg using the useSelect hook getting the core/editor使用 useSelect 挂钩获取核心/编辑器的 wordpress Gutenberg 打字稿类型
【发布时间】:2023-02-10 11:12:29
【问题描述】:

我正在尝试在 wordpress 和古腾堡编辑器的开发中使用打字稿。我正在尝试使用 useSelect() 挂钩,但在尝试使用 core/editor 的附加函数时出现类型错误。所以这是我遇到的问题的一个例子:

const featured = useSelect(
  (select) =>
    select('core/editor').getEditedPostAttribute('meta')[
      '_projects_featured'
    ],
  []
)

我收到以下错误:

“从不”类型上不存在属性“getEditedPostAttribute”。

我假设是因为没有应用正确的类型,因为无论我尝试从 select('core/editor') 获得什么,我都会得到同样的错误。但我不确定我应该应用哪种类型。我可以将选定的 core/editor 设置为 any,例如:

const editor: any = useSelect((select) => select('core/editor'))
const postAttribute = editor.getEditedPostAttribute()

但这显然违背了使用 typescript 的目的。

关于使用 wordpress 和 typescript 进行开发的信息并不多,也没有官方的 Wordpress typescript 文档。我添加了一堆打字,我将从我的package.json 发布其中的一些。

"dependencies": {
  "@types/wordpress__components": "^19.10.5",
  "@types/wordpress__core-data": "^2.4.5",
  "@types/wordpress__data": "^6.0.1",
  "@types/wordpress__edit-post": "^4.0.1",
  "@types/wordpress__editor": "^11.0.0",
  "@types/wordpress__element": "^2.14.1",
  "@types/wordpress__plugins": "^3.0.0",
  "@wordpress/data": "^8.0.0",
  "@wordpress/edit-post": "^7.0.0",
  "@wordpress/plugins": "^5.0.0"
}

任何帮助,将不胜感激。我不确定在 Wordpress Gutenberg 中使用 typescript 进行开发是否是个好主意。

【问题讨论】:

    标签: reactjs typescript wordpress wordpress-gutenberg


    【解决方案1】:

    仔细检查 useSelect() 是否在脚本开头导入:

    import { useSelect } from '@wordpress/data';
    
    const featured = useSelect(...);
    

    如果问题仍然存在,请检查您正在查询的帖子类型是否已注册给定的元键。注意:以下划线为前缀的元键,如_projects_featuredprivate/hidden custom field,不会出现在后期编辑中——这可能是这个特定元字段的潜在问题。 const featured = useSelect(...) 的语法很好,所以另一个潜在的问题可能是当您使用它时选择尚未解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 2020-05-09
      • 2021-11-25
      • 2022-11-14
      • 1970-01-01
      相关资源
      最近更新 更多