我正在使用一些 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"
]
}
最佳答案
finally
是 es2018
规范的一部分。您需要将 target
设置为 2018
或者您需要包含 es2018
库。
"compilerOptions": {
"target": "es2018",
...
}
或
"compilerOptions": {
"lib": [
"es2018",
"dom",
"scripthost"
]
...
}
这两个选项之间的区别在于编译器是否会使用 target
选项生成与 es2018
兼容的 JavaScript 代码(即它不会向下编译语言功能)或编译器是否只是假定规范的运行时特性存在(由指定的库定义),但它仍会将语言特性向下编译到您指定的任何目标(如果您使用 lib
)
关于angular - 错误 TS2339 : Property 'finally' does not exist on type 'Promise<void>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53204047/