Javascript 我可以从Save类访问源meta属性吗?
我已经为一个客户端构建了一个插件,它允许用户在每篇文章中选择一些颜色和一组字体,然后在每块文章中应用这些颜色和字体 信息存储在post的元数据中 这是通过post内部的侧面板完成的,因为页面上的所有块都需要访问数据,所以信息存储在post的元数据中 从侧面板存储和检索信息很好,从每个块的编辑类检索信息也很好(块不需要更改数据) 不幸的是,我在Save类中遇到了问题。 我知道我不能在Save类中使用data模块和with select,但是文档中的信息让我相信,如果我将信息定义为一个带有source“meta”的属性,我可以以与普通属性相同的方式使用它 “从这里,元属性可以由块使用与任何属性相同的接口进行读取和写入…” 这就是我成功地使用编辑类所做的,但它不适用于保存类。相反,这些属性根本不可用(即,当我在控制台上记录这些属性时,它不包括这些属性) 我当时的想法 我认为文档的意思一定是“仅在编辑类中”,但我想知道是否有其他人确实知道 代码 我认为代码不是很相关,因为除了Save类之外,它在任何地方都可以工作,但以下是我的属性定义供参考:Javascript 我可以从Save类访问源meta属性吗?,javascript,wordpress,wordpress-gutenberg,gutenberg-blocks,Javascript,Wordpress,Wordpress Gutenberg,Gutenberg Blocks,我已经为一个客户端构建了一个插件,它允许用户在每篇文章中选择一些颜色和一组字体,然后在每块文章中应用这些颜色和字体 信息存储在post的元数据中 这是通过post内部的侧面板完成的,因为页面上的所有块都需要访问数据,所以信息存储在post的元数据中 从侧面板存储和检索信息很好,从每个块的编辑类检索信息也很好(块不需要更改数据) 不幸的是,我在Save类中遇到了问题。 我知道我不能在Save类中使用data模块和with select,但是文档中的信息让我相信,如果我将信息定义为一个带有sourc
attributes: {
buttons: {
type: "array",
source: "query",
selector: "a",
query: {
text: {
type: "string",
source: "html",
attribute: "a"
},
url: {
type: "string",
source: "attribute",
attribute: "href"
},
colorKey: {
type: "string",
source: "attribute",
attribute: "data-color-key"
},
},
},
fontSetKey: {
type: "string",
source: "meta",
meta: "eimMeta_fontSetKey",
},
primaryColor: {
type: "string",
source: "meta",
meta: "eimMeta_primaryColor",
},
primaryContrastingShade: {
type: "string",
source: "meta",
meta: "eimMeta_primaryContrastingShade",
},
secondaryColor: {
type: "string",
source: "meta",
meta: "eimMeta_secondaryColor",
},
secondaryContrastingShade: {
type: "string",
source: "meta",
meta: "eimMeta_secondaryContrastingShade",
},
},
这是我的保存函数
import { Component } from "@wordpress/element";
import { RichText } from "@wordpress/editor";
import plugin from "../../plugin";
import category from "../category";
import block from "./block";
import { preparePostMetaForUse, getColorHexWithKey } from "../../common/values";
class Save extends Component {
render() {
const { attributes } = this.props;
const { className, buttons } = attributes;
console.log("Save attributes:");
console.log(attributes);
const postMeta = preparePostMetaForUse( attributes );
let jsxButtons = buttons.map((button) => ( <>
<RichText.Content
tagName="a"
value={button.text}
href={button.url}
data-color-key = {button.colorKey}
role="button"
target="_blank"
rel="noopener noreferrer"
style={{
fontFamily: postMeta.fontSet.body.fontFamily,
fontWeight: postMeta.fontSet.body.fontWeight,
fontSize: postMeta.fontSet.body.fontSize,
letterSpacing: postMeta.fontSet.body.letterSpacing,
lineHeight: postMeta.fontSet.body.lineHeight,
color: getColorHexWithKey(button.colorKey, postMeta.colorSet).contrastingShade,
backgroundColor: getColorHexWithKey(button.colorKey, postMeta.colorSet).color,
}}
/>
</> ));
return <div className={className}>{jsxButtons}</div>;
}
}
export default Save;
从“@wordpress/element”导入{Component};
从“@wordpress/editor”导入{RichText};
从“../../plugin”导入插件;
从“./类别”导入类别;
从“/block”导入块;
从“../../common/values”导入{preparePostMetaForUse,getColorHexWithKey};
类保存扩展组件{
render(){
const{attributes}=this.props;
常量{className,buttons}=属性;
log(“保存属性:”);
console.log(属性);
const postMeta=preparePostMetaForUse(属性);
让jsxButtons=buttons.map((button)=>)
));
返回{jsxButtons};
}
}
导出默认保存;