You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:12:52