Sweetalert2: Can't make sweetalert2 work on my angular2 project.

Created on 15 Nov 2016  路  4Comments  路  Source: sweetalert2/sweetalert2

This could have something to do with #335, I don't know.

I'm trying to use sweetalert2 on my angular2 project. What I want to do is create an AlertService and use it to create the alerts. Here's what I'm doing:

import { Injectable } from '@angular/core';
import * as swal from 'sweetalert2';

// declare var swal:any;

export interface IAlertService {
    alert(message: string): void;
    info(message: string): void;
    error(message: string): void;
    success(message: string): void;
}

@Injectable()
export class AlertService implements IAlertService {
    alert(message: string): void {
        debugger;

    }
    info(message: string): void {

    }
    error(message: string): void {

    }
    success(message: string): void {

    }
}

But I cannot get swal to work. It is always a null reference. I tried the way it's written on the home page: import { default as swal } from 'sweetalert2' but it's not working as well. I've installed the typings but it's not working as well. Any ideas on how to make it work?

Thanks

help wanted

Most helpful comment

@eestein if you happen to use angular-cli for your project you can take a look at this implementation: https://github.com/joshterrill/ng2-cli-sweetalert2

I'm using it this way (referencing dist/sweetalert2.js from angular-cli.json) and that works, thought I'd rather not add files to angular-cli.json but just do an import 'sweetalert2' where I need it.

All 4 comments

Hi.
dist/sweetaler2.js In the file does not export.
Code form file:

if (window.Sweetalert2) window.sweetAlert = window.swal = window.Sweetalert2;

This module use in global scope.

Maybe better to do the export?

module.export = Sweetalert2;

@eestein if you happen to use angular-cli for your project you can take a look at this implementation: https://github.com/joshterrill/ng2-cli-sweetalert2

I'm using it this way (referencing dist/sweetalert2.js from angular-cli.json) and that works, thought I'd rather not add files to angular-cli.json but just do an import 'sweetalert2' where I need it.

@beeman that's cool, thanks :)

Done in v6.2.5 via #381.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

limonte picture limonte  路  3Comments

jharrvis picture jharrvis  路  3Comments

smachs picture smachs  路  3Comments

brabao picture brabao  路  3Comments

PrestaShark picture PrestaShark  路  3Comments