【发布时间】:2019-04-11 17:50:48
【问题描述】:
我正在努力通过使用一些 JHipster 生成的服务来增强我的登录组件,但我遇到了一个问题。
当我尝试提交登录表单时收到error TS2339: Property 'finally' does not exist on type 'Promise<void>'.。
这是产生错误的代码:login.component.ts
login() {
if (this.validate(this.form)) {
this.loginService
.login({
username: this.model.username,
password: this.model.password,
})
.then(() => {
this.redirectUser();
})
.catch(() => {
this.authNoticeService.setNotice('The username or password is incorrect', 'error');
})
.finally(() => {
this.spinner.active = false;
this.actionChange.next( this.action );
});
}
}
login.service.ts
login(credentials, callback?) {
const cb = callback || function() {};
return new Promise((resolve, reject) => {
this.authServerProvider.login(credentials).subscribe(
data => {
this.principal.identity(true).then(account => {
// After the login the language will be changed to
// the language selected by the user during his registration
if (account !== null) {
this.languageService.changeLanguage(account.langKey);
}
resolve(data);
});
return cb();
},
err => {
this.logout();
reject(err);
return cb(err);
}
);
});
}
我尝试从login.service.ts 添加返回类型的登录方法,如下所示:
login(credentials, callback?):Promise<any> {
但没有任何成功。
我做了一些研究,根据这个:https://stackoverflow.com/a/52098456/9026582 问题应该通过typescript: 2.7.0 解决。
我有typescript: 2.7.2版本,所以我猜不是这样。
也许问题与login(credentials, callback?) 方法中的回调有关?
编辑:原始 tsconfig.json 配置
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es5",
"typeRoots": [
"./node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}
tsconfig.app.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"baseUrl": "./",
"module": "es2015",
"types": []
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
【问题讨论】:
-
@SachilaRanawaka 请阅读我的整个问题,正如我在该主题之后所写的那样
标签: angular typescript promise jhipster