javascript – (Angular2)无法读取toastr的undefined属性’extend’

我正在尝试在angularjs 2中使用toastr作为服务,将在我的组件中注入,如下所述.当调用handleEvent函数时,我正在接收“无法读取未定义的属性’扩展’”.非常感谢对错误的任何建议和/或解释.

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

//events/events-list.components.ts

import { ToastrService } from '../common/toastr.service';
@Component ({
    selector: 'event-list',
    template: `
    <div>
        <h1> Upcoming Angular 2 Event </h1>
        <hr>
        <div class="row">
            <div *ngFor="let event_data of all_data" class="col-md-5">
                <event-thumbnail (click)="handleEvent(event_data.name)" [event]="event_data"></event-thumbnail>
    </div>  
            </div>
        </div>
    `
})

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”>< / script>

原因:toastr.js使用jQuery,因此jQuery应该在toastr.js之前加载

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐