Angular 8集成ngx-toastr报错:Object(...)不是函数及ɵɵinject未找到
解决Angular 8中ngx-toastr的
ɵɵinject未找到及类型错误问题 嘿,这个问题我太熟悉了——你碰到的核心问题就是ngx-toastr版本和你的Angular 8不兼容!ɵɵinject是Angular 9及以后版本才有的内部注入API,而你现在装的ngx-toastr最新版已经不再支持Angular 8了,所以才会出现编译警告和运行时的类型错误。
下面给你一步步的解决方法:
1. 卸载当前不兼容的ngx-toastr版本
打开终端,执行卸载命令:
npm uninstall ngx-toastr --save
2. 安装适配Angular 8的特定版本
针对Angular 8,你需要安装11.x.x系列的ngx-toastr,这个版本是专门对Angular 8做兼容的,我推荐用11.3.3这个稳定版:
npm install ngx-toastr@11.3.3 --save
3. 确认已有配置无需修改
你之前做的这些配置都是正确的,不用调整:
- 已安装
@angular/animations并在app.module.ts里导入了BrowserAnimationsModule - 已在
app.module.ts中配置ToastrModule.forRoot() - 已在
angular.json中引入node_modules/ngx-toastr/toastr.css样式文件 - 组件里注入
ToastrService并调用this.toastr.success()的逻辑没问题
4. 清理缓存并重启项目
最后清理下npm缓存,再重新启动项目:
npm cache clean --force ng serve --open
这样操作完,控制台的错误和编译警告应该就都消失了,toastr提示也能正常工作啦!
内容的提问来源于stack exchange,提问作者abubakkar tahir
相关产品推荐
相关产品推荐

