如何在NativeScript中使用core JS导入和使用模块 问题:使用核心Javascript从NativeScript使用NPM模块

如何在NativeScript中使用core JS导入和使用模块 问题:使用核心Javascript从NativeScript使用NPM模块,javascript,nativescript,node-modules,Javascript,Nativescript,Node Modules,我是NativeScript新手,正在尝试使用FancyAlert NPM模块: 在自述文件中,他们展示了一个使用TypeScript的非常简单的示例,如下所示: import { TNSFancyAlert, TNSFancyAlertButton } from "nativescript-fancyalert"; . . . public showSuccess() { TNSFancyAlert.showSuccess( "Success!", "Fancy

我是NativeScript新手,正在尝试使用FancyAlert NPM模块:

在自述文件中,他们展示了一个使用TypeScript的非常简单的示例,如下所示:

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

在他们的XML文件中,他们只是简单地将其称为普通文件:

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

有人能帮我使用core JS使用这个模块吗

谢谢。

试试:fancyAlert.TNSFancyAlert.showSuccess

[编辑]这确实应该有效,因为它反映了Narendra Mongiya在您的问题中链接的解决方案。需要记住的一点是,这是一个承诺,所以让我们在其中添加一个catch块来揭示任何错误

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);
});

您还可能发现您只是从代码中的错误位置调用它。

@JohnS。为了澄清,我进行了编辑;“现在怎么样?”约翰说。我注意到在您的JS示例中,fancy alert调用的上下文不清楚;您正在将其包装到showSuccess函数中,对吗?请记住,必须在XML和JS文件之间正确绑定showSuccess,才能使其正常工作。有关TypeScript和JavaScript的更多示例,请参阅文档。您还可以参考将您在别处找到的任何TS代码片段转换为JS.first,非常感谢您花时间帮助我。现在,这很奇怪。。。。我使用了你的代码,应用程序运行,但没有警报,也没有错误。这是在操场上:你看到我在哪里出错了吗?你的操场显示错误,错误:[TNSFancyAlert]showsucture失败。引用错误:找不到变量:SCLARTERVIEW
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);
});