Angular 18迁移新构建系统后出现Http解析失败错误
Angular 18升级后In-Memory Web API失效问题排查
问题背景
将作为应用依赖库的项目从Angular 17升级到18,执行以下更新命令完成包升级与新构建系统迁移:
npm i typescript@5.5.4 ng update @angular/core@18 ng update @angular/cli@18 ng update @angular-eslint/schematics@18 ng update @angular/material@18
运行库时浏览器控制台出现HTTP解析错误,导致数据无法正常显示:
Http failure during parsing for http://localhost:4200/api/dashboard { "headers": { "normalizedNames": {}, "lazyUpdate": null }, "status": 200, "statusText": "OK", "url": "http://localhost:4200/api/dashboard", "ok": false, "name": "HttpErrorResponse", "message": "Http failure during parsing for http://localhost:4200/api/dashboard", "error": { "error": {}, "text": "<!doctype html>\r\n<html lang=\"en\">\r\n<head>\n <script type=\"module\" src=\"/@vite/client\"></script>\n\r\n <meta charset=\"utf-8\">\r\n <title>WidgetsApp</title>\r\n <base href=\"/\">\r\n\r\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\r\n <link rel=\"icon\" type=\"image/x-icon\" href=\"favicon.ico\">\r\n <link href=\"https://fonts.googleapis.com/icon?family=Material+Icons\" rel=\"stylesheet\">\r\n<link rel=\"stylesheet\" href=\"styles.css\"></head>\r\n<body>\r\n <app-root></app-root>\r\n<script src=\"polyfills.js\" type=\"module\"></script><script src=\"scripts.js\" defer></script><script src=\"main.js\" type=\"module\"></script></body>\r\n</html>\r\n" } }
代码细节
获取Mock数据的方法仅打印第一个console.log,tap操作符中的日志未执行:
const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'ngsw-bypass': 'true' }) }; getAllDashboard(): Observable<Dashboard[]> { const url = 'api/dashboard'; console.log("TEST (DashboardAPI) getAllDashboard"); return this._http.get<Dashboard[]>(url, httpOptions) .pipe( tap(dashboards => { console.log("TEST (DashboardAPI) getAllDashboard", dashboards); }), catchError(this._handleError<Dashboard[]>(`getAllDashboard url=${url}`)) ); }
实现InMemoryDbService的Mock服务在库自身运行正常,但在依赖该包的其他项目中失效:
export class WidgetInMemoryDataService implements InMemoryDbService { createDb() { const dashboardConfig = [ { id: '00000000-0000-0000-0000-000000000000', name: 'Default', creationDate: new Date(Date.now()), createdById: '00000000-0000-0000-0000-000000000000', updatedDate: new Date(Date.now()), updatedById: '00000000-0000-0000-0000-000000000000', } ]; return { dashboard: dashboardConfig }; } }
已在模块的imports中配置该服务:
HttpClientInMemoryWebApiModule.forRoot(WidgetInMemoryDataService, { dataEncapsulation: false, passThruUnknownUrl: true, }),
尝试过的无效方案
- 在
httpOptions中添加responseType: 'text'、responseType: 'text' as 'json',问题未解决 - 定位到问题与
HttpClientModule替换为provideHttpClient(withInterceptorsFromDi())相关:使用旧的HttpClientModule时Mock服务正常,而另一个依赖包@my-lib/dashboards使用新配置却能正常运行
项目依赖配置(package.json)
{ "name": "widgets-app", "version": "0.0.0", "license": "MIT", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "jest --coverage --verbose --silent", "lint": "ng lint", "build_prod": "ng build widgets --configuration production", "pack": "cd dist/widgets && npm pack", "package": "npm run build_prod && npm run pack", }, "private": true, "dependencies": { "@angular/animations": "^18.2.7", "@angular/cdk": "^18.2.7", "@angular/common": "^18.2.7", "@angular/compiler": "^18.2.7", "@angular/core": "^18.2.7", "@angular/forms": "^18.2.7", "@angular/material": "^18.2.7", "@angular/material-moment-adapter": "^18.2.7", "@angular/platform-browser": "^18.2.7", "@angular/platform-browser-dynamic": "^18.2.7", "@angular/router": "^18.2.7", "@asymmetrik/ngx-leaflet": "^18.0.1", "@mylib/dashboards": "file:../MyLib.Dashboards/dist/dashboards/mylib-dashboards-18.0.0.tgz", "@mylib/layout": "^18.0.2", "@ngx-translate/core": "^15.0.0", "@ngx-translate/http-loader": "^8.0.0", "@syncfusion/ej2-angular-pivotview": "^27.1.48", "angular-in-memory-web-api": "0.18.0", "echarts": "^5.4.3", "leaflet": "^1.9.4", "leaflet.markercluster": "^1.5.3", "moment": "^2.29.4", "ng-dynamic-component": "^10.7.0", "ngx-daterangepicker-material": "^6.0.4", "ngx-echarts": "^18.0.0", "ngx-skeleton-loader": "^9.0.0", "ngx-translate-multi-http-loader": "^18.1.0", "rxjs": "^7.8.1", "uuid": "^9.0.0", "zone.js": "~0.14.10" }, "devDependencies": { "@angular-devkit/build-angular": "^18.2.7", "@angular-eslint/builder": "^18.3.1", "@angular-eslint/eslint-plugin": "^18.3.1", "@angular-eslint/eslint-plugin-template": "^18.3.1", "@angular-eslint/schematics": "^18.3.1", "@angular-eslint/template-parser": "^18.3.1", "@angular/cli": "^18.2.7", "@angular/compiler-cli": "^18.2.7", "@ngneat/spectator": "^19.0.0", "@types/jest": "^29.5.13", "@types/leaflet": "^1.9.12", "@typescript-eslint/eslint-plugin": "^8.6.0", "@typescript-eslint/parser": "^8.6.0", "eslint": "^9.12.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-prettier": "^5.2.1", "jest": "^29.7.0", "jest-auto-spies": "^3.0.0", "jest-preset-angular": "^14.2.4", "jest-sonar": "^0.2.16", "ng-mocks": "^14.13.1", "ng-packagr": "^18.2.1", "prettier": "^3.3.3", "typescript": "~5.5.4" } }
内容的提问来源于stack exchange,提问作者Ihescode
相关产品推荐
相关产品推荐

