【问题标题】:toastr is not working with angular 8 it,s give me errortoastr 不适用于 angular 8 它,给我错误
【发布时间】:2020-04-12 02:52:42
【问题描述】:

我正在使用 angular 8 toastr,但我在控制台中收到此错误

ngx-toastr.js:264 Uncaught TypeError: Object(...) is not a function
at ngx-toastr.js:264
at Module../node_modules/ngx-toastr/fesm5/ngx-toastr.js (ngx-toastr.js:271)
at __webpack_require__ (bootstrap:78)
at Module../src/app/app.module.ts (app.component.ts:8)
at __webpack_require__ (bootstrap:78)
at Module../src/main.ts (main.ts:1)
at __webpack_require__ (bootstrap:78)
at Object.0 (main.ts:13)
at __webpack_require__ (bootstrap:78)
at checkDeferredModules (bootstrap:45)
(anonymous) @ ngx-toastr.js:264
./node_modules/ngx-toastr/fesm5/ngx-toastr.js @ ngx-toastr.js:271
__webpack_require__ @ bootstrap:78

./src/app/app.module.ts@app.component.ts:8 webpack_require @bootstrap:78 ./src/main.ts@main.ts:1 webpack_require @bootstrap:78 0@main.ts:13 webpack_require @bootstrap:78 checkDeferredModules @ bootstrap:45 webpackJsonpCallback @ bootstrap:32 (匿名)@ main.js:1 client:148 [WDS] 编译时出现警告。

我已经在命令行中运行了这些

 npm install ngx-toastr --save
 npm install @angular/animations --save

在我的 app.module.ts 中,我已经包含了这些行

  import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
  import { ToastrModule } from 'ngx-toastr';

   imports: [
     BrowserModule,
     AppRoutingModule,
     BrowserAnimationsModule,
     ToastrModule.forRoot()
    ],

我已经在 angular.json 中包含了这个

 "node_modules/ngx-toastr/toastr.css"

这是我的 component.ts 文件

   import { ToastrService } from 'ngx-toastr';
   constract(public toastr: ToastrService){}
   ngOnInit() {
  this.toastr.success('hello world', 'Success!');
  }

当我使用 ng serve 命令运行角度服务器时,它会显示此错误

  WARNING in ./node_modules/ngx-toastr/fesm5/ngx-toastr.js 368:198-206
  "export 'ɵɵinject' was not found in '@angular/core'

【问题讨论】:

标签: angular roaster


【解决方案1】:

我在控制台中出现错误的原因是我的 Angular cli 版本是 7 的烘焙器版本,而我使用的是 11 版的烘焙器,所以这就是我安装烘焙器@7.1.0 时版本依赖于 angular cli 的原因所以一切正常且完美。

【讨论】:

    【解决方案2】:

    不知道您是否从代码中进行了简单的复制/粘贴,但以下行是错误的:

     constract(public toastr: ToastrService){}
    

    替换为

     constructor(public toastr: ToastrService){}
    

    但我不确定它能否解决您的问题,因为如果它解决了您的代码甚至不会像这样编译。

    也许你可以尝试 npm cache clean --force 和 npm install 来下载依赖项

    【讨论】:

    • 是的,我注意到了。他应该通过构造函数注入服务。
    猜你喜欢
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2014-02-10
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多