0

I am using systemjs.config, angular 4 and ng2-toastr version 4.0.1 I got an error when running the app "Cannot read property 'forRoot' of undefined" It seems does not load the ToastModule or any class from ng2-bootstrap

This is the systemjs.config.js file

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'npm:': 'node_modules/'

        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'app',

            // angular bundles
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            //'@angular/platform-browser/animations': 'npm:@angular/platform-browser/bundles/platform-browser-animations.umd.min.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
            'ng2-toastr': 'npm:ng2-toastr/bundles/ng2-toastr.min.js',
            'ng2-validation': 'npm:ng2-validation/bundles/ng2-validation.umd.js',
            'libphonenumber-js': 'npm:libphonenumber-js/bundle/libphonenumber-js.min.js',

            // other libraries
            'rxjs': 'npm:rxjs',
            'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            },
            'ng2-toastr': {
                defaultExtension: 'js'
            },
                'ng2-validation': {
                    defaultExtension: 'js'
                }
        }
    });
})(this);

This is the core module where I load toastr

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { CustomFormsModule } from 'ng2-validation'
import { ToastModule } from 'ng2-toastr';

@NgModule({
imports: [BrowserModule, CustomFormsModule, ToastModule.forRoot()],
providers: [}],
declarations: [],
bootstrap: []
})
export class CoreModule { }
malballah
  • 681
  • 1
  • 9
  • 17

2 Answers2

1

I have faced the same problem. Finally, I found the solution. You can define ng2-toastr in system.config.js as follows.

In map section,

    map: {
    app: 'app',
     ....
     'ng2-toastr': 'npm:ng2-toastr'
    }

then In packages section,

'ng2-toastr': { main: './bundles/ng2-toastr.min.js', defaultExtension: 'js'}

In app.module.ts, you have to import toastr like this.

import { ToastModule } from 'ng2-toastr/ng2-toastr';

In index.html, you have to load css and javascript file.

<link href="~/node_modules/ng2-toastr/bundles/ng2-toastr.min.css" rel="stylesheet" /> <script src="~/node_modules/ng2-toastr/bundles/ng2-toastr.min.js"></script>

Finally, In components, you can imports Toaster components.

import { ToastsManager, ToastOptions } from 'ng2-toastr/ng2-toastr';

It works for me.

Thanks.

Dhanabal K
  • 11
  • 1
0

Try 'ng2-toastr': 'npm:ng2-toastr' instead of 'ng2-toastr': 'npm:ng2-toastr/bundles/ng2-toastr.min.js' in the map section

LanceM
  • 1,888
  • 4
  • 23
  • 41