【问题标题】:Passing attributes from parent to child Gutenberg Blocks将属性从父级传递给子级 Gutenberg Blocks
【发布时间】:2021-06-28 06:49:10
【问题描述】:

如何传递父属性或传递内部子块中的一些道具?

<InnerBlocks allowedBlocks={[ 'groupama-blocks/block-08']}/>

例如,我想将此块属性添加到块 08,我将添加为内部块。

【问题讨论】:

    标签: wordpress attributes parent-child wordpress-gutenberg gutenberg-blocks


    【解决方案1】:

    块上下文是“registerBlockType”中使用的 Wordpress 功能,它允许祖先块向后代块提供值。

    https://developer.wordpress.org/block-editor/reference-guides/block-api/block-context/

    【讨论】:

      【解决方案2】:

      将数据从父母传递给孩子 GUTENBERG

      家长

      const {useSelect, dispatch, select} = wp.data;
      
      edit: (props) => {
          const {
              clientId
          } = props;
      
          var children = select('core/block-editor').getBlocksByClientId(clientId).[0].innerBlocks;
          children.forEach(function(child){
              dispatch('core/block-editor').updateBlockAttributes(child.clientId, {label: 'Hello'})
          });
      

      孩子

      let parent = select('core/block-editor').getBlockParents(clientId);
      const parentAttributes = select('core/block-editor').getBlockAttributes(parent);
      console.log(parentAttributes); // Block Attributes
      console.log(props.attributes.label); // Passing Props
      

      【讨论】:

      • 非常感谢这个例子!我正在做一些相反的事情——当父母发生变化时添加或删除孩子——但这个例子帮助我理解了dispatch,所以我可以实现我的目标。
      猜你喜欢
      • 2018-01-15
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多