登录AngularJS Meteor后重定向
我尝试在使用AngularJS登录Meteor中的特定页面后重定向。但不知何故,它不起作用。登录Meteor.user()后返回null。正因为如此,每次它都只能路由到消息页面。我在一个论坛上看到了这个例子,并在此基础上开发了这个例子登录AngularJS Meteor后重定向,angularjs,meteor,angular-ui-router,angular-meteor,Angularjs,Meteor,Angular Ui Router,Angular Meteor,我尝试在使用AngularJS登录Meteor中的特定页面后重定向。但不知何故,它不起作用。登录Meteor.user()后返回null。正因为如此,每次它都只能路由到消息页面。我在一个论坛上看到了这个例子,并在此基础上开发了这个例子 angular.module("jaarvis").run(["$rootScope", "$state", "$meteor", function($rootScope, $state, $meteor) { $meteor.autorun($rootS
angular.module("jaarvis").run(["$rootScope", "$state", "$meteor", function($rootScope, $state, $meteor) {
$meteor.autorun($rootScope, function(){
if (! Meteor.user()) {
console.log('user');
if (Meteor.loggingIn()) {
console.log('loggingIn ' + Meteor.user()); -- returning null
if(Meteor.user()) {
$state.go('onlineusers');
} else {
//On login
$state.go("messages");
}
}
else{
console.log('login');
$state.go('login');
}
}
});
}]);
路线声明如下
angular.module('jaarvis').config(['$urlRouterProvider', '$stateProvider', '$locationProvider',
function($urlRouterProvider, $stateProvider, $locationProvider){
$locationProvider.html5Mode(true);
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'login.ng.html',
controller: 'LoginCtrl'
})
.state('onlineusers',{
url: '/onlineusers',
templateUrl: 'client/onlineusers/onlineusers.ng.html',
controller: 'OnlineUsersCtrl'
})
.state('messages', {
url: '/messages',
templateUrl: 'client/chats.ng.html',
controller: 'ChatCtrl'
})
});
$urlRouterProvider.otherwise("/messages");
}]);
使用下面的代码片段记录日志
<meteor-include src="loginButtons"></meteor-include>
可能是用户对象尚未加载。您可以尝试:
if ( Meteor.userId() ) ...
相反迈克尔关于这个问题的根本原因可能是对的,但我认为角流星提供了一个更好的选择 你要做的是在路上强行解决一个承诺。根据角流星文件(即一般示例…): 您的特定代码如下所示:
angular.module('jaarvis').config(['$urlRouterProvider', '$stateProvider', '$locationProvider',
function($urlRouterProvider, $stateProvider, $locationProvider){
$locationProvider.html5Mode(true);
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'login.ng.html',
controller: 'LoginCtrl'
})
.state('onlineusers',{
url: '/onlineusers',
templateUrl: 'client/onlineusers/onlineusers.ng.html',
controller: 'OnlineUsersCtrl',
resolve: {
"currentUser": ["$meteor", function($meteor){
return $meteor.waitForUser();
}]
}
})
.state('messages', {
url: '/messages',
templateUrl: 'client/chats.ng.html',
controller: 'ChatCtrl',
resolve: {
"currentUser": ["$meteor", function($meteor){
return $meteor.waitForUser();
}]
}
})
});
$urlRouterProvider.otherwise("/messages");
}]);
然后在ChatCtrl
和OnlineUsersCtrl
控制器上,添加currentUser
作为要注入的变量之一,如:
angular.module("rootModule").controller("ChatCtrl", ["$scope", "$meteor", ...,
function($scope, $meteor, ..., "currentUser"){
console.log(currentUser) // SHOULD PRINT WHAT YOU WANT
}
]);
您可能还想考虑“代码> $GASOR.RealUsReUR())/>代码承诺,然后如果承诺被拒绝,将用户发送回登录页面。所有这些都在angular meteor网站上有很好的记录
祝你好运 您是否使用angular meteor自定义登录?它们返回一个承诺,允许您重定向
$meteor.loginWithPassword(username,pass).then(function(){$state.go('where'))代码>@Tj Gienger我尝试使用自定义登录,但回调方法未调用$然后(函数(){console.log('Login success');},函数(err){console.log('Login error-',err);});我试图注入currentUser,但它抛出以下异常。错误:[$injector:unpr]未知提供程序:currentUserProvider抱歉,您是对的,我有一个输入错误,我将在回答中编辑。总之:不要注入“currentUser”——UI路由器解析已经为我们处理了它,只需将它作为变量放入scope函数中即可。应该是:angular.module(“rootModule”).controller(“ChatCtrl”[“$scope”,“$meteor”,function($scope,$meteor,,,“currentUser”){console.log(currentUser)//应该打印所需内容}]
删除当前用户的依赖项注入--resolve已经为usI解决了这个问题。我使用了您的一些建议并改进了我的应用程序。在上面的示例中,大多数情况下currentUser为null或未定义。我使用了Accounts.onLogin()用于重定向预期页面的功能。感谢您宝贵的时间和建议。我可以尝试这种方法,但我认为需要再次获取用户对象并检查isAdmin参数。我对meteor相当陌生,但对meteor.userId()感到困惑
因为虽然它在本地环境中非常适合我,但当我尝试在heroku生产服务器上执行相同的操作时,它通常是未定义的。Meteor.userId()
在客户端使用,而在服务器方法和发布中使用this.userId
。
angular.module("rootModule").controller("ChatCtrl", ["$scope", "$meteor", ...,
function($scope, $meteor, ..., "currentUser"){
console.log(currentUser) // SHOULD PRINT WHAT YOU WANT
}
]);