我遇到了一个大问题,经过几个小时的阅读文档和解决方案后,我找不到如何解决这个问题:
基本上我在我的角度守卫中有这个:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.checkLogin(route);
}
checkLogin(route) {
return this.authService.login().map(response => {
if (this.authService.isAuth) {
return this.authService.grants().map(grants => {
if (grants.canRead) {
return true;
}
})
}
});
}
我想要做的是首先调用服务authService.login,如果我已经认证,那么我调用authService.grants来检查用户是否可以阅读此页面。
就这么简单,但是我无法进入第二个电话,我不知道为什么它会返回一个可观察的。
答案 0 :(得分:1)
如果没有方法的定义,我会假设以下内容:
message: 'Login failed for user \'DIR\\maja.okholm\'.',
code: 'ELOGIN' },
name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
问题是,目前 router.get('/', function (req, res, next) {
var sql = require("mssql");
// config for your database
var config = {
user: 'DIR\\maja.okholm',
password: '******',
server: 'localhost', //CPX-Q2N4C7MBZ9L\SQLEXPRESS01 localhost (local)\SQLEXPRESS01
database: 'Retracer Pages'
};
// connect to your database
let connection = sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request(connection);
// query to the database and get the records
let result = request.query('select * from Configurations', function (err, recordset) {
if (err) console.log(err)
// send records as a response
//console.log(recordset);
res.send(recordset);
});
});
方法的返回类型为interface Rights {
canRead: boolean;
}
class AuthService {
grants(): Observable<Rights>{}
login(): Observable<any>{}
}
。这是因为:
checkLogin
为Observable<boolean>| Observable<Observable<boolean>>
,外部地图将产生this.authService.isAuth
,因为您只需返回false
。Observable<boolean>
为false
,外部地图将产生this.authService.isAuth
,因为您现在将返回另一个异步的映射值。操作因此,使用它,您应该将您的警卫重构为以下内容:
true
MergeMap基本上允许您连接异步操作,并创建可观察的同步。值。