John S.
John S.

Reputation: 514

How do I import and use module using core JS in NativeScript

Problem: Using NPM Modules From NativeScript using Core Javascript

I am new to NativeScript and am trying to use the FancyAlert NPM module:

https://github.com/NathanWalker/nativescript-fancyalert

In their readme, they show a very simple example using TypeScript, like this:

import { TNSFancyAlert, TNSFancyAlertButton } from "nativescript-fancyalert";
.
.
.
public showSuccess() {
    TNSFancyAlert.showSuccess(
      "Success!",
      "Fancy alerts are nice.",
      "Yes they are!"
    );
  }

and in their XML file, they simply call it like normal:

 <Button text="Alert Success" tap="{{showSuccess}}" />

How to convert that to core JS

So I am not using TypeScript. I cannot get this simple alert to work. I have tried:

in my main-view-model.js

const fancyAlert = require("nativescript-fancyalert");

.
.
.

fancyAlert.showSuccess(
                    "Success!",
                    "You are logged in.",
                    "Success!"
                  );

not working.

Can anyone help me to use this module using core JS?

Thank you.

Upvotes: 0

Views: 247

Answers (1)

Jamie Birch
Jamie Birch

Reputation: 6112

Try: fancyAlert.TNSFancyAlert.showSuccess().

[EDIT] This really should work, as it's a mirror of the solution given in the issue filing linked by Narendra Mongiya in your question. One thing to keep in mind is that it's a Promise, so let's add a catch() block in there to reveal any errors.

const fancyAlert = require("nativescript-fancyalert");

// ...

fancyAlert.TNSFancyAlert.showSuccess(
    "Success!",
    "You are logged in.",
    "Success!"
)
.then((resolution) => {
    console.log("[TNSFancyAlert] showSuccess() succeeded.", resolution);    
})
.catch((error) => {
    console.error("[TNSFancyAlert] showSuccess() failed.", error);
});

You may also find that you're simply calling it from the wrong place in your code.

Upvotes: 1

Related Questions