如何修复错误 "this.accountService is undefined"
How to fix an error "this.accountService is undefined"
我是 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/<@ng:///NgrkAppModule/NavbarComponent.ngfactory.js:1470:23
core/login/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;
}
--------------------------------
}
如果 this
处理不当,读取 this
的内容并发现它是 undefined
是一个常见问题。
修复
快速修复是将 an arrow 用于 logout
:
logout = () => {
并可能在其他地方使用它(例如 login
)以及为了将来的安全。
您可以在 Promise 外部保留对此的引用并使用它来代替它。
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 class
@Injectable()
export class AccountService
我是 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/<@ng:///NgrkAppModule/NavbarComponent.ngfactory.js:1470:23
core/login/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;
}
--------------------------------
}
如果 this
处理不当,读取 this
的内容并发现它是 undefined
是一个常见问题。
修复
快速修复是将 an arrow 用于 logout
:
logout = () => {
并可能在其他地方使用它(例如 login
)以及为了将来的安全。
您可以在 Promise 外部保留对此的引用并使用它来代替它。
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 class
@Injectable()
export class AccountService