当用户在登录后被重定向到仪表板时,我想使用这个简单的UI模式。
因此,如果用户注销-他只能看到主页。登录后,他被重定向至/dashboard。
如何使用angular2路由器和meteor帐号包实现此目的?
提前感谢您的帮助。
发布于 2016-08-28 14:06:37
不幸的是,当login和logout方法被触发时,accounts-ui包似乎没有公开生命周期钩子或任何其他明显的方式来运行额外的代码。
所以这也不是最优雅的解决方案,但它是有效的-使用angular2-meteor-accounts ui(在GitHub和NPM上可用):
import { Component, DoCheck } from '@angular/core';
import { Router, ROUTER_DIRECTIVES } from '@angular/router';
import 'rxjs/Rx';
import { Meteor } from 'meteor/meteor';
import { MeteorComponent } from 'angular2-meteor';
import { InjectUser, LoginButtons } from 'angular2-meteor-accounts-ui';
import template from './app.component.html';
@Component({
selector: 'app',
template,
directives: [ LoginButtons, ROUTER_DIRECTIVES ]
})
@InjectUser('user')
export class AppComponent extends MeteorComponent implements DoCheck {
private isLoggedIn: boolean = false;
private user: Meteor.User;
constructor( private router: Router ) { super(); }
ngDoCheck() {
if (!this.isLoggedIn && !!this.user) {
this.isLoggedIn = true;
if (this.router.url === '/') {
this.router.navigate( ['/dashboard'] );
}
} else if (this.isLoggedIn && this.user === null) {
this.isLoggedIn = false;
if (this.router.url === '/dashboard') {
this.router.navigate( ['/'] );
}
}
}
}Meteor论坛上还有一篇文章描述了一种不同的解决方案,不使用accounts-ui包,而是创建自己的login()方法,如下所示:
发布于 2016-01-05 22:48:34
这可能不是最优雅的解决方案,但我最终使用autorun在登录后重定向用户。
$scope.autorun(() => {
let currentUser = Meteor.user();
if (currentUser)
$state.go('home');
});https://stackoverflow.com/questions/34612952
复制相似问题