Javascript 如何重定向到快速获取
我正在为我的webapp使用Express和React,因为我们正在将渲染从Handlebar迁移到React。但是,我们希望在Express中维护逻辑,并仅在React中渲染。问题是当我想重定向时。这是client.js,具有以下逻辑:Javascript 如何重定向到快速获取,javascript,reactjs,express,react-router,Javascript,Reactjs,Express,React Router,我正在为我的webapp使用Express和React,因为我们正在将渲染从Handlebar迁移到React。但是,我们希望在Express中维护逻辑,并仅在React中渲染。问题是当我想重定向时。这是client.js,具有以下逻辑: import React from 'react'; import ReactDOM from 'react-dom'; import App from './components/App'; import { BrowserRouter } from "re
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
import { BrowserRouter } from "react-router-dom";
// In production, we want to hydrate instead of render
// because of the server-rendering
if (process.env.NODE_ENV === 'production') {
ReactDOM.hydrate(
<BrowserRouter>
<App {...window.__APP_INITIAL_STATE__} />
</BrowserRouter>
, document.getElementById('app'));
} else {
ReactDOM.render(
<BrowserRouter>
<App {...window.__APP_INITIAL_STATE__} />
</BrowserRouter>
, document.getElementById('app'));
}
// Hot reload is that easy with Parcel
if (module.hot) {
module.hot.accept();
}
从“React”导入React;
从“react dom”导入react dom;
从“./components/App”导入应用程序;
从“react router dom”导入{BrowserRouter};
//在制作中,我们希望使用水合物而不是渲染
//因为服务器渲染
if(process.env.NODE_env===‘生产’){
水合物(
,document.getElementById('app');
}否则{
ReactDOM.render(
,document.getElementById('app');
}
//包裹热装有那么容易吗
如果(模块热){
module.hot.accept();
}
这是一条捷径:
app.get('/signup', (req,res)=>{
const initialState = {component:'SignUp'};
let context = {};
const markup = ReactDOM.renderToString(
<StaticRouter location={req.url} context={context}>
<App {...initialState} />
</StaticRouter>
);
if (context.url) {
res.writeHead(302, {
Location: context.url
});
res.end();
}
else {
res.send(generateHtml(markup,initialState));
}
});
app.get('/signup',(req,res)=>{
常量initialState={组件:'SignUp'};
让上下文={};
const markup=ReactDOM.renderToString(
);
if(context.url){
文书标题(302{
位置:context.url
});
res.end();
}
否则{
res.send(generateHtml(标记,初始状态));
}
});
问题是:当我尝试这样重定向(并更改此.state.redirect)时:
render(){
是否返回此.state.redirect?:(
嗨);
}
浏览器转到/登录路径,但不调用服务器。它显示一个空白页
这也是我的应用程序组件:
export default class App extends Component {
constructor(props){
super(props);
console.log(props);
}
render(){
if(this.props.component === 'Root'){
return <Root />;
}
else if(this.props.component === 'Login'){
return <Login />;
}
else if(this.props.component === 'SignUp'){
return <SignUp />;
}
else{
return (
<div>
<h1>Hello word {this.props.component}</h1>
<Button color="danger">Danger!</Button>
</div>
);
}
}
}
导出默认类应用程序扩展组件{
建造师(道具){
超级(道具);
控制台日志(道具);
}
render(){
if(this.props.component=='Root'){
返回;
}
else if(this.props.component=='Login'){
返回;
}
else if(this.props.component=='SignUp'){
返回;
}
否则{
返回(
您好word{this.props.component}
危险!
);
}
}
}
这是一个根组件,用于管理去向
如何解决这个问题?我不想使用react路由器来处理路由,我们想在Express上维护所有的路由。
谢谢您可以在子域上托管您的express应用程序:
<Redirect to="http://express.myapp.com/login" />
当客户端呈现时,您可以
window.location.replace(“/login”)代码>
要模拟
:window.location.href=“/login”代码>
在服务器端,您可以继续使用
,您通常希望使用客户端路由或服务器端路由,而不是both@BoyWithSilverWings什么意思?
是在客户端进行路由,因此它不会向客户端发出请求backend@BoyWithSilverWings这是我想解决的问题,你怎么能这么做?我不能给你发帖子express@BoyWithSilverWings我也遵循了这个指南,可以做我想做的事情:
<Redirect to="http://express.myapp.com/login" />