我在快速会话中使用Nuxt SSR,并且我从服务器端重定向了护照JS
/**
* POST /signup
* Create a new local account.
*/
exports.postSignup = (req, res, next) => {
const validationErrors = [];
if (!validator.isEmail(req.body.email)) validationErrors.push({ msg: 'Please enter a valid email address.' });
if (!validator.isLength(req.body.password, { min: 8 })) validationErrors.push({ msg: 'Password must be at least 8 characters long' });
if (req.body.password !== req.body.confirmPassword) validationErrors.push({ msg: 'Passwords do not match' });
if (validationErrors.length) {
req.flash('errors', validationErrors);
return res.redirect('/signup');
}
req.body.email = validator.normalizeEmail(req.body.email, { gmail_remove_dots: false });
const user = new User({
email: req.body.email,
password: req.body.password
});
User.findOne({ email: req.body.email }, (err, existingUser) => {
if (err) { return next(err); }
if (existingUser) {
req.flash('errors', { msg: 'Account with that email address already exists.' });
return res.redirect('/signup');
}
user.save((err) => {
if (err) { return next(err); }
req.logIn(user, (err) => {
if (err) {
return next(err);
}
res.redirect('/');
});
});
});
};
如果我调用重定向方法?它将重新加载页面并清除Vuex状态,对吗?我该如何从护照重定向,以使Vuex状态保持不变且客户端页面不刷新
确实最好异步处理表单提交以避免@Darius提到的页面刷新。但是,为了完善起见,我想提到确实存在一些解决方案来保持您的Vuex状态,例如vuex-persistedstate。它可以用于将状态持久保存到localStorage,sessionStorage甚至cookie。它也可以用作Nuxt插件。