关于javascript:(Angular2)无法读取toastr的undefined属性’extend’

(Angular2) Cannot read property 'extend' of undefined for toastr

我正在尝试将angularjs 2中的toastr用作一项服务,如下所述将其注入到我的组件中。调用handleEvent函数时,我收到"无法读取未定义的属性\\'extend \\'"。对该错误的任何建议和/或解释,我们深表感谢。

app.moudle.ts已导入ToastrService,并且ToastrService已添加到提供程序中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//events/events-list.components.ts

import { ToastrService } from '../common/toastr.service';
@Component ({
    selector: 'event-list',
    template: `
   
         Upcoming Angular 2 Event
       
       
           
                <event-thumbnail (click)="handleEvent(event_data.name)" [event]="event_data"></event-thumbnail>
     
           
       
    `
})

export class EventListComponent implements OnInit {
  all_data:any[]
  constructor(private eventService : EventService , private toastr : ToastrService ){
    //moved code from here to ngOnInit
  }

  ngOnInit(){
    this.all_data = this.eventService.getEvents();
  }

  handleEvent(eventName) {
    console.log("hey here "+eventName);
    this.toastr.success(eventName);
  }
}

错误:
[在console.log输出之后抛出此错误]

EXCEPTION: Error in ./EventListComponent class EventListComponent -
inline template:6:16 caused by: Cannot read property 'extend' of
undefined.
ORIGINAL EXCEPTION: Cannot read property 'extend' of undefined
TypeError: Cannot read property 'extend' of undefined
at m (toastr.js:411)
at Object.s [as success] (toastr.js:85)
at ToastrService.success (toastr.service.ts:12)


添加此
script src="node_modules/toastr/build/toastr.min.js"

在此之后
script src="node_modules/jquery/dist/jquery.min.js">

原因:toastr.js使用jQuery,因此jQuery应该在toastr.js

之前加载


这对我有用:

1
2
3
4
5
"scripts": [
 "node_modules/jquery/dist/jquery.min.js",
 "node_modules/toastr/build/toastr.min.js",
 "node_modules/bootstrap/dist/js/bootstrap.js"
]

以完全相同的顺序在脚本部分下的angular.json文件中。


  • 打开终端-> npm install jquery --save
  • 转到angular.json文件,添加以下内容:
  • 1
    2
    3
    4
    "scripts": [
       "node_modules/jquery/dist/jquery.min.js",
       "node_modules/toastr/build/toastr.min.js"
    ]

    在使用toastr之前,请确保导入jquery。它是对烤面包机的要求。


    bootstrap.min.css也应该在toastr.min.js之前下注。

    环境:Angular 7和bootstrap4。

    下面是angular.json的一部分,它正在工作。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
      "styles": [
         "node_modules/ngf-bootstrap/dist/bootstrap.min.css",
         "node_modules/toastr/build/toastr.min.css",
         "src/styles.css"
        ],
       "scripts": [
         "node_modules/jquery/dist/jquery.min.js",
         "node_modules/bootstrap/dist/js/bootstrap.js",
         "node_modules/toastr/build/toastr.min.js"
        ]