Original Toastr with Angular 5?

user441058 picture user441058 · Dec 11, 2017 · Viewed 7.2k times · Source

I like the original toastr b/c I could pop up a "Item X Saved" message and it stays on the page for X milliseconds after I redirect back to the home page. So I would like to use the original toastr library with Angular 5 and can't figure it out. Is there a blog post or online sample that shows this?

Note: I know that there are npm packages like angular2-toaster and a few others, but when I try to install and run them using Angular 5 CLI there are numerous error messages and when I look at their Github page they have outstanding issues and it just seems that Ag5 had enough breaking changes where they just aren't working yet without analyzing the source code and hacking it out (and I'm no npm expert obviously). Seems like they worked with Ag4 but not with Ag5?

Since toastr is a standard JS library, why can't I just add a link to the min.js file in angluar-cli.json "scripts" and show a standard popup like the good old days before SPA? I've downloaded and fought with so many of these npm libraries tonight it seems like it has been much harder than it should be to use a standard library that has been around for years.

Thanks.

Answer

user4676340 picture user4676340 · Dec 11, 2017

I highly recommend you Angular Material to do that.

Once you've installed it (it works with Angular 5 and the installation procedure is very easy to follow), all you need is

private constructor(private snacker: MatSnackBar) {}
// ...
toast() { this.snacker.open('Text to display', 'Button text', { duration: 2000 }); }