如果帖子摘录为空,我正在尝试在 Gutenberg 编辑器中添加帖子保存验证。我现在拥有的是:
const { subscribe } = wp.data;
const unsubscribe = subscribe(() => {
const editor = wp.data.select('core/editor');
const notices = wp.data.select('core/notices');
const isSavingPost = editor.isSavingPost();
const isAutosavingPost = editor.isAutosavingPost();
const didPostSaveRequestSucceed = editor.didPostSaveRequestSucceed();
if (isSavingPost && !isAutosavingPost && didPostSaveRequestSucceed) {
const postExcerpt = editor.getEditedPostAttribute('excerpt');
if (postExcerpt.length !== 0) {
editor.unlockPostSaving('excerpt-lock');
notices.removeNotice('excerpt-lock');
editor.savePost();
}
editor.lockPostSaving('excerpt-lock');
notices.createNotice(
'error',
'Please add an excerpt',
{ id: 'excerpt-lock', isDismissible: false }
);
unsubscribe();
}
});
但在帖子保存我得到
Uncaught (in promise) TypeError: editor.lockPostSaving 不是一个函数
我不确定我错过了什么。我在各种教程中查看了 GB 存储库中的大量示例,但没有任何效果。