服务器端反应路由器 onEnter 处理,transition.redirect 不是函数
Server side react router onEnter handling, transition.redirect is not a function
对于我的同构应用程序,我使用了反应路由器 (2.0.1) 并且我需要处理身份验证,所以我使用了 onEnter 钩子,根据文档 api。我需要处理身份验证但得到
TypeError: transition.redirect is not a function
routes.jsx 文件
**/**
*
* @param next
* @param transition
* @param callback
*/
function requireAuth (next, transition, callback) {
if (!window.user){
transition.abort();
transition.redirect ({
pathname: '/login',
state: { nextPathname: next.location.pathname }
}, transition);
}
else {
callback(null, transition);
}
}**
<Router history={history} onUpdate={onUpdate} onEnter={requireAuth}>
<Route path="/rt/" component={RealTime}/>
<Route path="/" component={Blank}/>
{devRoutes}
</Router>
server.js 文件
app.get('*', function(req, res, next) {
Router.run(routes(), location, function(e, i, t) {
var str = React.renderToString(
React.createElement(Router, i));
res.send (str)
}
}
我认为你的 requireAuth 应该是这样的
function requireAuth (next, transition, callback) {
if (!window.user){
transition({
pathname: '/login',
state: { nextPathname: next.location.pathname }
});
}
else {
callback();
}
}
您可以看到 here 转换不调用方法,如果 'if' 语句失败,您只需传递空回调以继续页面
没有transition.redirect函数,我需要修改onEnter transition hook中的transition redirectInfo对象使用transition.to 然后在主服务器处理函数中验证。
/**
*
* @param next
* @param transition
* @param callback
*/
function requireAuth (next, transition, callback) {
var loginPath = '/account/login/' ;
if (!window.user && !(next.location.pathname === loginPath)){
transition.abort();
transition.to(loginPath, null, {
//data: req.body,
//errors: res.body
})
}
callback()
}
重定向不在 onEnter 挂钩上处理,而是在主服务器获取处理程序中处理。
app.get('*', function(req, res, next) {
Router.run(routes(), location, function(e, i, transition) {
// **********************************************
// handling redirection info
if (transition.isCancelled) {
console.log ('transition cancelled');
return res.redirect(302, transition.redirectInfo.pathname);
}
// ********************************************************
var str = React.renderToString(
React.createElement(Router, i));
res.send (str)
}
}
对于我的同构应用程序,我使用了反应路由器 (2.0.1) 并且我需要处理身份验证,所以我使用了 onEnter 钩子,根据文档 api。我需要处理身份验证但得到
TypeError: transition.redirect is not a function
routes.jsx 文件
**/**
*
* @param next
* @param transition
* @param callback
*/
function requireAuth (next, transition, callback) {
if (!window.user){
transition.abort();
transition.redirect ({
pathname: '/login',
state: { nextPathname: next.location.pathname }
}, transition);
}
else {
callback(null, transition);
}
}**
<Router history={history} onUpdate={onUpdate} onEnter={requireAuth}>
<Route path="/rt/" component={RealTime}/>
<Route path="/" component={Blank}/>
{devRoutes}
</Router>
server.js 文件
app.get('*', function(req, res, next) {
Router.run(routes(), location, function(e, i, t) {
var str = React.renderToString(
React.createElement(Router, i));
res.send (str)
}
}
我认为你的 requireAuth 应该是这样的
function requireAuth (next, transition, callback) {
if (!window.user){
transition({
pathname: '/login',
state: { nextPathname: next.location.pathname }
});
}
else {
callback();
}
}
您可以看到 here 转换不调用方法,如果 'if' 语句失败,您只需传递空回调以继续页面
没有transition.redirect函数,我需要修改onEnter transition hook中的transition redirectInfo对象使用transition.to 然后在主服务器处理函数中验证。
/**
*
* @param next
* @param transition
* @param callback
*/
function requireAuth (next, transition, callback) {
var loginPath = '/account/login/' ;
if (!window.user && !(next.location.pathname === loginPath)){
transition.abort();
transition.to(loginPath, null, {
//data: req.body,
//errors: res.body
})
}
callback()
}
重定向不在 onEnter 挂钩上处理,而是在主服务器获取处理程序中处理。
app.get('*', function(req, res, next) {
Router.run(routes(), location, function(e, i, transition) {
// **********************************************
// handling redirection info
if (transition.isCancelled) {
console.log ('transition cancelled');
return res.redirect(302, transition.redirectInfo.pathname);
}
// ********************************************************
var str = React.renderToString(
React.createElement(Router, i));
res.send (str)
}
}