我有一个小项目reactjs
要玩和学习。我需要输入将基于 url 路径显示的标题。所以以下是我处理路由的index.js:
const history = useRouterHistory(createHistory)({
basename: '/test'
})
class Tj extends React.Component {
render() {
return (
<Router history={history}>
<Route path={"/"} component={Bridge} >
<IndexRoute component={Home} />
<Route path={"user"} component={T} />
<Route path={"home"} component={Home} />
</Route>
<Route path={"/t/"} component={Bridge2} >
<IndexRoute component={T} />
<Route path={"contact"} component={Home} />
</Route>
</Router>
);
}
}
render(
<Provider store={store}>
<Tj/>
</Provider>,
window.document.getElementById('mainContainer'));
如您所见,我使用 test 作为根目录,并根据用户对 url 的输入决定应该使用哪个标头。这里还有Bridge2.js:
export class Bridge2 extends React.Component {
render() {
return (
<div>
<div className="row">
<Header2/>
</div>
<div className="row">
{this.props.children}
</div>
</div>
);
}
}
和Bridge.js:
export class Bridge extends React.Component {
render() {
// alert(this.props.children.type);
var Content;
if(this.props.children.type){
Content=<div>
<div className="row">
<Header/>
</div>
<div className="row">
{this.props.children}
</div>
</div>;
}
else{
Content=<div>
<div className="row">
<Header/>
</div>
<div className="row">
{this.props.children}
</div>
</div>;
}
return (
Content
);
}
}
当我在 webpack 开发服务器中运行它时,一切正常。例如,当我使用http://localhost:3003/test/
bridge.js 时加载,如果我运行http://localhost:3003/test/t/
bridge2.js 加载这是预期的。
但是,由于 web pack 开发服务器不是生产服务器,我使用 tomcat,现在我使用 eclipse web 应用程序项目,并将我的 bundle.js 文件和 index.html 复制到那里。现在的问题是,当我运行 tomcat 服务器时,它能够识别并正常显示此路径:
http://localhost:8080/test/
但是当http://localhost:8080/test/t/
我得到:
HTTP 状态 404 - /test/t/
这基本上说资源文件不可用。据我观察,这在编码中不是问题,因为路由在 webpack 开发服务器中工作正常,但是当涉及到 tomcat 时,似乎react路由无法处理它。我在做什么有什么问题吗?这种方式完全可行吗?