Open In App

How To Use ngx toastr in Angular17 ?

Last Updated : 02 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Angular, we have a styling component called ngx-toastr, widely used for displaying non-blocking notifications to the user. This component helps enhance the user experience by providing feedback for various actions like success, error, info, and warning messages. By integrating ngx-toastr with Angular, you can create reusable and maintainable notification systems within your application.

In this article, we will see the proper use ngx-toastr in Angular. We have added the example that represents the use of ngx-toastr in Angular.

Steps To Use ngx toastr in Angular

Step 1: Install Angular CLI

If you haven’t installed Angular CLI yet, install it using the following command

npm install -g @angular/cli

Step 2: Create a New Angular Project

ng new toastrApp --standalone
cd toastrApp

Step 3: Create Standalone Component

Create a standalone component to use ngx-toastr. You can generate a standalone component using the Angular CLI:

ng generate component Toastr --standalone

Step 3: Install Dependencies

Install ngx-toastr and its dependencies:

npm install ngx-toastr @angular/animations

Project Structure

PS
Project Structure

Dependencies

"dependencies": {
"@angular/animations": "^17.3.12",
"@angular/common": "^17.3.12",
"@angular/compiler": "^17.3.12",
"@angular/core": "^17.3.12",
"@angular/forms": "^17.3.12",
"@angular/platform-browser": "^17.3.12",
"@angular/platform-browser-dynamic": "^17.3.12",
"@angular/router": "^17.3.12",
"ngx-toastr": "^19.0.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.8"
}

Example

HTML
<!--toastr.component.html-->

<h1>GeeksforGeeks</h1>
<h3>Approach: Using ngx-toastr with Standalone Component</h3>

<button (click)="showSuccess()">Show Success Toast</button>
<button (click)="showError()">Show Error Toast</button>
HTML
<!--index.html-->

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>ToastrApp</title>
    <base href="/" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="styles.css" />
</head>

<body>
    <app-toastr></app-toastr>
</body>

</html>
CSS
/* styles.css */
@import "../node_modules/ngx-toastr/toastr.css";
h1 {
  color: green;
}
JavaScript
// toastr.component.ts

import { Component } from '@angular/core';
import { ToastrService } from 'ngx-toastr';

@Component({
    selector: 'app-toastr',
    templateUrl: './toastr.component.html',
    styleUrls: ['./toastr.component.css'],
    standalone: true,
})
export class ToastrComponent {
    constructor(private toastr: ToastrService) { }

    showSuccess() {
        this.toastr.success('Success message', 'Success');
    }

    showError() {
        this.toastr.error('Error message', 'Error');
    }
}
JavaScript
// main.ts

import { importProvidersFrom } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ToastrModule } from 'ngx-toastr';
import { ToastrComponent } from './app/toastr/toastr.component';

bootstrapApplication(ToastrComponent, {
    providers: [
        importProvidersFrom(BrowserAnimationsModule),
        importProvidersFrom(ToastrModule.forRoot()),
    ],
}).catch((err) => console.error(err));
JavaScript
//angular.json
{
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "version": 1,
    "newProjectRoot": "projects",
    "projects": {
        "toastrApp": {
            "projectType": "application",
            "schematics": {
                "@schematics/angular:component": {
                    "standalone": true
                },
                "@schematics/angular:directive": {
                    "standalone": true
                },
                "@schematics/angular:pipe": {
                    "standalone": true
                }
            },
            "root": "",
            "sourceRoot": "src",
            "prefix": "app",
            "architect": {
                "build": {
                    "builder": "@angular-devkit/build-angular:browser",
                    "options": {
                        "outputPath": "dist/toastr-app",
                        "index": "src/index.html",
                        "main": "src/main.ts",
                        "polyfills": [
                            "zone.js"
                        ],
                        "tsConfig": "tsconfig.app.json",
                        "assets": [
                            "src/favicon.ico",
                            "src/assets"
                        ],
                        "styles": [
                            "src/styles.css"
                        ],
                        "scripts": []
                    },
                    "configurations": {
                        "production": {
                            "budgets": [
                                {
                                    "type": "initial",
                                    "maximumWarning": "500kb",
                                    "maximumError": "1mb"
                                },
                                {
                                    "type": "anyComponentStyle",
                                    "maximumWarning": "2kb",
                                    "maximumError": "4kb"
                                }
                            ],
                            "outputHashing": "all"
                        },
                        "development": {
                            "buildOptimizer": false,
                            "optimization": false,
                            "vendorChunk": true,
                            "extractLicenses": false,
                            "sourceMap": true,
                            "namedChunks": true
                        }
                    },
                    "defaultConfiguration": "production"
                },
                "serve": {
                    "builder": "@angular-devkit/build-angular:dev-server",
                    "configurations": {
                        "production": {
                            "buildTarget": "toastrApp:build:production"
                        },
                        "development": {
                            "buildTarget": "toastrApp:build:development"
                        }
                    },
                    "defaultConfiguration": "development"
                },
                "extract-i18n": {
                    "builder": "@angular-devkit/build-angular:extract-i18n",
                    "options": {
                        "buildTarget": "toastrApp:build"
                    }
                },
                "test": {
                    "builder": "@angular-devkit/build-angular:karma",
                    "options": {
                        "polyfills": [
                            "zone.js",
                            "zone.js/testing"
                        ],
                        "tsConfig": "tsconfig.spec.json",
                        "assets": [
                            "src/favicon.ico",
                            "src/assets"
                        ],
                        "styles": [
                            "src/styles.css",
                            "src/assets/toastr.css"
                        ],
                        "scripts": []
                    }
                }
            }
        }
    },
    "cli": {
        "analytics": "997b24f9-2302-4a79-a839-ea4127eaa68b"
    }
}


Steps to run this Project

ng serve

Output



Similar Reads