Angular 11:为什么编译器在编译过程中找不到"地理位置位置"? "Cannot find name 'GeolocationPosition'"



为什么angular编译器找不到GeolocationPositionGeolocationPositionError?VSC不会给出错误,只有在编译过程中它才会给我一个错误。

Error: src/app/modules/shared/services/position.service.ts:10:46 - error TS2304: Cannot find name 'GeolocationPosition'.
10   private positionSource = new ReplaySubject<GeolocationPosition>(1);

我通过使用any类型来解决这个问题,但我只是好奇Angular为什么会给我这个错误,以及我如何解决这个问题。

我已经尝试过不同的编译器目标(es5es6es2018),但没有成功。已经安装了@types/core-js,但也没有任何运气。我可能缺少什么@types模块吗?

这是我当前的tsconfig.json

{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "es5",
"module": "es2020",
"lib": [
"es2018",
"dom"
]
},
"angularCompilerOptions": {
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}

在typescript 4.0.x中,这种类型被称为Position。在typescript 4.1.x中,它被重命名为GeolocationPosition

我想你会发现你的VS代码使用的是内置版本的typescript,而你的项目使用的是早期版本。

要修复:

npm install --save-dev typescript@4.1.2

我遇到了同样的问题,我解决了更改项目使用的本地Angular CLI的问题
解释
我更新到Angular 11,但忘记更新Angular CLI。ng build给了我这个输出:

Your global Angular CLI version (9.1.7) is greater than your local
version (9.1.0). The local Angular CLI version is used.

步骤
更新Angular CLI(如有必要)

npm install -g @angular/cli

更新npm

npm install -g npm

ng build输出变为:

Your global Angular CLI version (11.1.2) is greater than your local version (9.1.0). The local Angular CLI version is used.

安装npm检查更新

npm i -g npm-check-updates

执行ncu -u以查看要更新的包
执行npm install以更新所有程序包
在这之后,一切都应该正常。

我的解决方案是将ol和ol类型的版本从"^6.5.0";精确版本";6.5.0";,

"ol":"6.5.0";,@types/ol":"6.5.0";,

较新的软件包具有GeolocationPositionError类型的突破性更改。

Angular 9-带字体~3.8.3

如果您使用的是低于4的typescript版本,则将GeolocationPosition替换为Position将解决此问题。

最新更新