Mil*_*Rpo 5 iframe reactjs react-router
我有一个反应项目,我有一个横幅库,我使用反应路由器4在不同的客户,年和活动之间导航
<BrowserRouter>
<div>
<Switch>
<Route path="/:client/:year/:campaign/:banner" exact component={bannerPage} />
<Route path="/:client/:year/:campaign" exact component={campaignPage} />
<Route path="/:client/:year" exact component={campaignIndex} />
<Route path="/" exact component={clientIndex} />
</Switch>
</div>
</BrowserRouter>
Run Code Online (Sandbox Code Playgroud)
在显示特定横幅的组件上,我有一个i帧来渲染实际横幅而没有别的
render() {
return(
<div>
<iframe src='data/client/year/campaign/banner'
width="300px"
height="250px"
display="initial" >
</iframe>
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
但当我尝试渲染横幅时,React Router拦截iframe的src并再次显示index.html页面,没有任何组件,因为路径与任何路径都不匹配
任何帮助,将不胜感激!谢谢!
我不完全确定你所说的“拦截 iframe 的 src”是什么意思,但我假设你指的是服务器上的行为,因为我看不出你所描述的内容与客户端有何关系边代码。
一般来说,服务器上 React Router 的工作方式是使用 catch all 处理程序来匹配除静态内容之外的所有内容。您可以通过在捕获所有内容之前定义静态内容来避免静态内容。
const app = express();
app.use('/static', express.static(__dirname + '/static'));
app.get('*', (req, res) => {
res.sendFile(__dirname + '/static/index.html')
});
app.listen('8000');
Run Code Online (Sandbox Code Playgroud)
现在,如果您需要在应用程序请求时提供一些不同的内容/data/client/year/campaign/banner
,则需要在捕获所有内容之前添加一个处理程序。如果没有有关您的服务器设置的更多信息,我不确定具体会是什么,但如下所示:
const app = express();
app.use('/static', express.static(__dirname + '/static'));
app.use('/data', express.static(__dirname + '/data'));
app.get('*', (req, res) => {
res.sendFile(__dirname + '/static/index.html')
});
app.listen('8000');
Run Code Online (Sandbox Code Playgroud)