Angular项目遇TypeError及模块导出缺失警告求助
解决Angular项目中的
Object(...) is not a function及export 'inject'/'defineInjectable' was not found错误 从你提供的错误和警告信息来看,核心问题是Angular核心框架版本与Angular CDK/Material版本不匹配。你遇到的export 'inject' was not found in '@angular/core'、export 'defineInjectable' was not found in '@angular/core'以及TypeError: Object(...) is not a function这些问题,都是因为你使用的Angular CDK/Material版本依赖了Angular 6+才引入的API,但你的Angular核心版本低于这个版本,导致无法找到这些导出项。
具体错误信息回顾:
platform.es5.js:79 Uncaught TypeError: Object(...) is not a function at eval (platform.es5.js:79) at eval (platform.es5.js:81) at Object../node_modules/@angular/cdk/esm5/platform.es5.js (vendor.bundle.js:63) at __webpack_require__ (inline.bundle.js:55) // ... 省略后续调用栈 // 关键警告信息 ./node_modules/@angular/cdk/esm5/a11y.es5.js 1119:164-170 "export 'inject' was not found in '@angular/core'" ./node_modules/@angular/cdk/esm5/a11y.es5.js 1119:62-78 "export 'defineInjectable' was not found in '@angular/core'"
解决方案步骤:
第一步:确认当前Angular核心版本
打开项目根目录的package.json文件,找到@angular/core的版本号,或者在终端运行以下命令查看:ng version记下这个版本号(比如
6.1.10、7.2.15等)。第二步:统一CDK和Material的版本
Angular CDK和Material的版本必须与Angular核心版本完全一致。如果当前版本不匹配,先卸载现有的CDK和Material:npm uninstall @angular/cdk @angular/material然后安装与核心版本一致的版本,假设你的Angular核心版本是
x.y.z,运行:npm install @angular/cdk@x.y.z @angular/material@x.y.z第三步:清理缓存并重新构建
为了避免缓存导致的问题,清理npm缓存并重新构建项目:npm cache clean --force rm -rf node_modules package-lock.json npm install ng serve --open
额外注意事项:
- 如果你是升级了Angular核心版本但忘记同步CDK/Material版本,也会出现这个问题,所以每次升级Angular核心后,一定要同步升级CDK和Material。
- Angular Material和CDK始终与Angular核心版本保持同步,版本号完全一致,这是官方的兼容规则。
内容的提问来源于stack exchange,提问作者Mrinalini Pal
相关产品推荐
相关产品推荐

