【发布时间】:2022-06-20 08:03:17
【问题描述】:
我正在尝试在 Silverstripe CMS 实例中的页面模板 (Page.ss) 上呈现 React.js 组件。我有以下反应组件:
import styles from './myComponent.module.scss';
type Props = {
pageTitle?: string;
};
const ExampleReactComponent = ({ pageTitle = 'Home'}: Props): JSX.Element => {
return (
<div className={styles['example-sync-component']}>
This is a React component. Here is a prop: <em>{pageTitle}</em>
</div>
);
};
export default ExampleReactComponent;
这会呈现到 Page.ss(CMS 模板)中:
<div data-component="my-component"></div>
并正确显示 pageTitle 的值(例如 'Home')。但我希望能够做类似的事情:
<div data-component="my-component" data-page-title="$Title"></div>
... 其中 $Title 是 Silverstripe 模板上的一个变量,它将返回“Home”。 如何从模板变量中获取值到我的反应组件的道具中?谢谢。
【问题讨论】:
-
我认为您可能需要使用Injector
标签: reactjs react-props silverstripe