Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 等待继续-6_Angular_Typescript - Fatal编程技术网

Angular 等待继续-6

Angular 等待继续-6,angular,typescript,Angular,Typescript,我需要任何解决这个问题的办法。我需要读取从http请求接收数据的属性。因此,我只需要在事务完成时测试这个属性 checkAuthorization(path: string): boolean{ const usuarioLogado: UsrStoraged = this.storage.getUsuarioLogado(); let usuario: Usuario this.usuarioService.findByLogin(usuarioLogado.log

我需要任何解决这个问题的办法。我需要读取从http请求接收数据的属性。因此,我只需要在事务完成时测试这个属性

  checkAuthorization(path: string): boolean{
    const usuarioLogado: UsrStoraged = this.storage.getUsuarioLogado();
    let usuario: Usuario
    this.usuarioService.findByLogin(usuarioLogado.login).subscribe(
        data => {
            usuario = data
        }

    )

    if (usuario.perfil.springSecurity == 'ROLE_ADMIN'){
        return true;
    } else {
        const message: Message = {message: 'Usuário sem permissão', type: TipoMensagem.ERROR}
        this.message.notify(message)
        return false;

    }
}

data=>{
将在您收到它时被调用,因此您可以像这样将代码移到内部:

checkAuthorization(path: string): boolean{
    const usuarioLogado: UsrStoraged = this.storage.getUsuarioLogado();
    let usuario: Usuario
    this.usuarioService.findByLogin(usuarioLogado.login).subscribe(
        data => {
            usuario = data
            if (usuario.perfil.springSecurity == 'ROLE_ADMIN'){
                return true;
            } else {
                const message: Message = {message: 'Usuário sem permissão', type: TipoMensagem.ERROR}
                this.message.notify(message)
                return false;

            }
        } 
    );
}

提示:要处理错误案例,请在下面添加
错误=>{}
模拟到
数据。

您必须修改函数以返回可观察值,并使用
映射
将数据转换为布尔结果

checkAuthorization(path: string): Observable<boolean>{
    const usuarioLogado: UsrStoraged = this.storage.getUsuarioLogado();
    return this.usuarioService.findByLogin(usuarioLogado.login).pipe(map(
        data => {
            const usuario = data

            if (usuario.perfil.springSecurity == 'ROLE_ADMIN'){
                return true;
            } else {
                const message: Message = {message: 'Usuário sem permissão', type: TipoMensagem.ERROR}
                this.message.notify(message)
                return false;
            }
        }
    ))
}

然后,您必须修改调用此函数的任何代码以订阅结果。例如,代替

if (checkAuthorization("foo")) {
    // do stuff
} else {
    // do other stuff
}
你会用

checkAuthorization("foo").subscribe(isAuthorized => {
    if (isAuthorized) {
        // do stuff
    } else {
        // do other stuff
    }
})

subscribe
内部返回与从
checkAuthorization
返回不同这是否用于
RouteGuard
checkAuthorization("foo").subscribe(isAuthorized => {
    if (isAuthorized) {
        // do stuff
    } else {
        // do other stuff
    }
})