我正在通过使用一些由JHipster生成的服务来增强我的Login组件,但是遇到了问题。
当我尝试提交登录表单时,我得到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"
]
}
答案 0 :(得分:1)
finally
是 es2018
规范的一部分。您需要将 target
设置为 2018
,或者需要包含 es2018
lib。
“ compilerOptions”:{
“ target”:“ es2018”,
...
}
或
“ compilerOptions”:{
“ lib”:[
“ es2018”,
“ dom”,
“脚本主机”
]
...
}
这两个选项之间的区别在于,编译器是否会通过 target
选项发出与 es2018
兼容的JavaScript代码(即,不会下编译语言功能),还是编译器是否仅假定存在规范的运行时功能(由指定的libs定义),但仍会将语言功能向下编译为您指定的任何目标(如果使用 lib
)