Wrapping Libraries in Services JS

Posted: 2018-03-01 16:46:34

Just a note to self that when using libraries like sweetalert for example and then I want to convert to sweetalert2, it would have saved a lot of time to wrap it into a "service" and import that.

For example I made this js file

services/Alerts.js:

import swal from 'sweetalert2';

export default {

    send(title, message, error = false) {
        swal(
            {
                type: error ? "error" : "success",
                title: title,
                text: message
            }
        );
    }

}

Then import it into the app or into the component that needs it.

import Alerts from "./services/alerts.js";
window.$alerts = Alerts;

And later on in that file

let message = "Something went wrong with your token, please update it or email for support";
$alerts.alert("Error", message,  true);

Tags:

note2self