1

我有这个组件'PostTitle',它获取页面标题并在您编辑在编辑器中工作的主页标题时发生变化,但我无法让它显示在前端?不知道我做错了什么。

更新

我添加了一个函数来更新“标题”属性,但问题是它只在编辑时触发。因此,如果您不在编辑器中单击组件,它将不会更新“标题”属性。有没有办法把这个放到存档区?

registerBlockType( 'gutenberg/title', {
    title: 'updating text',
    icon: 'universal-access-alt',
    category: 'layout',
    attributes: {
        title: {
            type: 'array',
            source: 'children',
            selector: 'h1',
        },
    },

    edit( { attributes } ) {

        const { title } = attributes;
        const GetTitle = props => props.title;
    //I'm not sure how to get this to update the title attribute
        const selectTitle = withSelect(select => ({
            title: select("core/editor").getDocumentTitle(),
         }));
        const PostTitle = selectTitle(GetTitle);

       //update function that fire when you edit the component 
       function updateTitle() {
            const pagetitle = select("core/editor").getDocumentTitle();
            setAttributes( { title: pagetitle } );
            console.log(pagetitle);
        }
        updateTitle();

        const controls = (
            <Fragment>
                 .....
            </Fragment>
        );

        return (
            <Fragment>
                { controls }        
                <PostTitle/>
            </Fragment>
        );
    },

    save( { attributes } ) {
        const { title } = attributes;
        return (
            <div>
                <RichText.Content tagName="h1" className="title" value={ title } />
            </div>
        );
    },

} );

任何帮助将非常感激

4

0 回答 0