【发布时间】:2021-06-28 06:49:10
【问题描述】:
如何传递父属性或传递内部子块中的一些道具?
<InnerBlocks allowedBlocks={[ 'groupama-blocks/block-08']}/>
例如,我想将此块属性添加到块 08,我将添加为内部块。
【问题讨论】:
标签: wordpress attributes parent-child wordpress-gutenberg gutenberg-blocks
如何传递父属性或传递内部子块中的一些道具?
<InnerBlocks allowedBlocks={[ 'groupama-blocks/block-08']}/>
例如,我想将此块属性添加到块 08,我将添加为内部块。
【问题讨论】:
标签: wordpress attributes parent-child wordpress-gutenberg gutenberg-blocks
块上下文是“registerBlockType”中使用的 Wordpress 功能,它允许祖先块向后代块提供值。
https://developer.wordpress.org/block-editor/reference-guides/block-api/block-context/
【讨论】:
将数据从父母传递给孩子 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,所以我可以实现我的目标。