我正在尝试在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);
}
}
Error:
[在console.log输出后抛出此错误]
EXCEPTION:./ EventListComponent类中的错误EventListComponent - 内联模板:6:16导致:无法读取未定义的属性“extend” . 原始异常:无法读取未定义的属性'extend'TypeError:无法在ToastrService.success上的Object.s [as success](toastr.js:85)读取m(toastr.js:411)的undefined属性'extend'( toastr.service.ts:12)
1 回答
添加
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
之前加载