要解决Accounts.createUser()中重定向功能不起作用的问题,可以尝试以下解决方法:
import { Router } from 'meteor/iron:router';
Router.route('/signup', {
name: 'signup',
action() {
this.render('signup');
}
});
Template.signup.events({
'submit form': function(event) {
event.preventDefault();
var username = event.target.username.value;
var password = event.target.password.value;
Accounts.createUser({
username: username,
password: password
}, function(error) {
if (error) {
console.log(error.reason);
} else {
Router.go('/dashboard');
}
});
}
});
import { AccountsTemplates } from 'meteor/useraccounts:core';
AccountsTemplates.configure({
onSubmitHook: function(error, state) {
if (!error) {
if (state === 'signIn') {
Router.go('/dashboard');
}
}
}
});
Template.signup.events({
'submit form': function(event) {
event.preventDefault();
var username = event.target.username.value;
var password = event.target.password.value;
Accounts.createUser({
username: username,
password: password
});
}
});
服务器端:
import { Meteor } from 'meteor/meteor';
Meteor.methods({
createUserAndRedirect: function(username, password) {
Accounts.createUser({
username: username,
password: password
});
}
});
客户端:
Template.signup.events({
'submit form': function(event) {
event.preventDefault();
var username = event.target.username.value;
var password = event.target.password.value;
Meteor.call('createUserAndRedirect', username, password, function(error) {
if (error) {
console.log(error.reason);
} else {
Router.go('/dashboard');
}
});
}
});
通过以上方法,可以解决Accounts.createUser()中重定向功能不起作用的问题,并在用户注册成功后进行重定向。