当用户登录后重定向到仪表板时,我想使用这个简单的UI模式。
所以,若用户注销,他只能看到一个主页。登录后,他被重定向到/dashboard。
我如何使用angular2路由器和流星账户包做到这一点?
提前感谢您的帮助。
不幸的是,在触发登录和注销方法时,accounts-ui包似乎没有暴露生命周期挂钩或任何其他明显的运行额外代码的方式。
因此,这也不是最优雅的解决方案,但它是有效的——使用angular2流星账户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()方法,在这里:
https://forums.meteor.com/t/angular2-meteor-redirect-after-successful-login-for-custom-login-form/19312
这可能不是最优雅的解决方案,但我最终在登录后使用autorun重定向用户。
$scope.autorun(() => {
let currentUser = Meteor.user();
if (currentUser)
$state.go('home');
});