我正在使用一些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"
]
}
发布于 2018-11-08 08:40:48
finally
是es2018
规范的一部分。您需要将target
设置为2018
或包含es2018
库。
"compilerOptions": {
"target": "es2018",
...
}
或
"compilerOptions": {
"lib": [
"es2018",
"dom",
"scripthost"
]
...
}
这两个选项的不同之处在于编译器是否会发出与es2018
兼容的target
代码(即它不会向下编译语言特性)与target
选项兼容,或者编译器是否只是假设规范的运行时特性存在(由指定的lib定义),但它仍然会将编译语言特性降到您指定的任何目标(如果使用lib
)。
https://stackoverflow.com/questions/53204047
复制相似问题