【发布时间】:2016-12-26 07:03:29
【问题描述】:
我尝试使用 React 路由器中的链接在新选项卡中打开一个组件,它打开了一个 404 not found 而不是 react 组件,
React 入口点 js,
import React from 'react';
import { render } from 'react-dom';
import { Route,Router, browserHistory} from 'react-router';
import Home from './components/Home';
import About from './components/About';
render((
<Router history={browserHistory}>
<Route path="/Home" component={Home}></Route>
<Route path="/about" component={About}></Route>
</Router>
), document.getElementById('reactDiv'));
主页组件,
import React from 'react';
export default class Home extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h2> Home Page </h2>
<Link to={`/about`} target="_blank">
About
</Link>
</div>
);
}
}
关于组件
import React from 'react';
export default class About extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h2> About Page </h2>
</div>
);
}
}
我希望 About 组件会显示在新标签中,但它会打开一个新标签并出现 404 page not found 错误。
谁能告诉我如何在新标签页中打开组件。
【问题讨论】:
-
你必须将 `historyApiFallback' 设置为 true
-
我没有使用 webpack 开发服务器,那么如何设置这个 historyApiFallback 选项??
-
确保在服务器(任何)上,当服务器上没有路由匹配时,您提供包含 react 生成的 .js 文件的 html 文件。
-
您需要在 Home 组件中使用 this.props.children。
标签: reactjs react-router