reactjs - React-Router 和 iFrame
问题描述
当从 iFrame 调用 ReactJS 应用程序时,是否可以在 React-Router 中执行一些规则以仅显示某些路由?
我有以下路线,如您所见,它们将始终显示<Header />
和<Footer />
组件:
...
<Route component={Header} />
<Route exact path="/" component={Landing} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
<Route component={Footer} />
...
但是,当有人从 iFrame 将我的应用程序嵌入到他的网站(如小部件)中时,我不想显示<Header />
和<Footer />
组件,所以它的行为如下:
...
<Route exact path="/" component={Landing} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
...
解决方案
维护状态或道具是否嵌入 iframe。我在以下示例中使用了道具:
render() {
return (
<div>
{this.props.inIframe !== null && this.props.inInframe !== true ? <Header /> : null}
<Switch>
<Route exact path="/" component={Dashboard}/>
<Route path="/about" component={WebsiteEditor}/>
<Route path="/contact" component={WebsiteViewer} />
</Switch>
{this.props.inIframe !== null && this.props.inInframe !== true ? <Footer /> : null}
</div>
);
}
推荐阅读
- ssl - Nginx PEM_read_bio_X509 PEM_read_bio:错误的结束行
- mysql - 当我在一张表中有数百万条记录时如何提高性能?
- python - 在 pd.read_excel 之后如何用通用索引更改索引
- java - 无法解析 @OneToMany 子表 (@JoinColumn) 的属性
- java - Java - FileOutputStream 覆盖文件,但它似乎没有改变
- maven - 从 pom 中的所有依赖项中排除特定文件
- translation - Symfony 5:将阿拉伯数字翻译成现代数字
- android - Android Button 背景无法识别/使用可绘制对象
- javascript - 如何在 React 功能组件中立即更新状态?
- mysql - Rails 升级到 5.2.3 导致“Mysql2::Error: Too many connections”