【问题标题】:How to fix an error "this.accountService is undefined"如何修复错误“this.accountService 未定义”
【发布时间】:2019-05-08 04:02:06
【问题描述】:

我是 Angular 和 JHipster 的新手。请帮助解决问题。我没有更改那里的代码。有jhipster的默认登录码。

堆栈跟踪:

TypeError: this.accountService 未定义 堆栈跟踪: LoginService.prototype.logout@webpack-internal:///./src/main/webapp/app/core/login/login.service.ts:33:9 NavbarComponent.prototype.logout@webpack-internal:///./src/main/webapp/app/layouts/navbar/navbar.component.ts:49:9 View_NavbarComponent_30/

核心/登录/login.service.ts:

import { AccountService } from 'app/core/auth/account.service';

--------------------------------------------------

constructor(private accountService: AccountService)

--------------------------------------------------

    login(credentials, callback?) {
        const cb = callback || function() {};

        return new Promise((resolve, reject) => {
            this.authServerProvider.login(credentials).subscribe(
                data => {
                    this.accountService.identity(true).then(account => {
                        resolve(data);
                    });
                    return cb();
                },
                err => {
                    this.logout();
                    reject(err);
                    return cb(err);
                }
            );
        });
    }


    logout() {
        this.authServerProvider.logout().subscribe();
        this.accountService.authenticate(null);
    }

core/auth/account.service.ts:

export class AccountService {
--------------------------------

    authenticate() {
      some code;
    }

    identity() {
      some code;
    }

--------------------------------
}

【问题讨论】:

    标签: angular typescript login jhipster


    【解决方案1】:

    如果this 处理不当,从this 读取某些内容并发现它是undefined 是一个常见问题。

    修复

    快速解决方法是将an arrow 用于logout

    logout = () => {
    

    并且可能在其他地方使用它(例如login)以及未来的安全。

    【讨论】:

    • 感谢回电。调用注销也报错
    • 酷。我会尽量不缩小范围,只推荐直接logout 修复;)?
    【解决方案2】:

    您可以在 Promise 之外保留 this 的引用并使用它来代替 this。

    login(credentials, callback?) {
        let that = this;
        const cb = callback || function() {};
    
        return new Promise((resolve, reject) => {
            that.authServerProvider.login(credentials).subscribe(
                data => {
                    that.accountService.identity(true).then(account => {
                        resolve(data);
                    });
                    return cb();
                },
                err => {
                    that.logout();
                    reject(err);
                    return cb(err);
                }
            );
        });
    }
    

    还将@Injectable() 添加到您的 AccountService 类中

     @Injectable()
    export class AccountService
    

    【讨论】:

    • 感谢回电。我刚试过,对我不起作用。注销时也会出错
    • Firefox:that.accountService 未定义。相同的堆栈跟踪。 Chrome:无法读取未定义的属性“身份验证”
    • 您是否将@Injectable() 添加到服务类?
    • 你们找到解决方案了吗?
    猜你喜欢
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多