简体   繁体   English

如何在NativeScript中使用核心JS导入和使用模块

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

Problem: Using NPM Modules From NativeScript using Core Javascript 问题:使用核心Javascript从NativeScript使用NPM模块

I am new to NativeScript and am trying to use the FancyAlert NPM module: 我是NativeScript的新手,正在尝试使用FancyAlert NPM模块:

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

In their readme, they show a very simple example using TypeScript, like this: 在自述文件中,他们展示了一个使用TypeScript的非常简单的示例,如下所示:

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: 在他们的XML文件中,他们像正常一样简单地调用它:

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

How to convert that to core JS 如何将其转换为核心JS

So I am not using TypeScript. 所以我不使用TypeScript。 I cannot get this simple alert to work. 我无法获得此简单的警报来工作。 I have tried: 我努力了:

in my main-view-model.js 在我的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? 谁能帮助我使用核心JS使用此模块?

Thank you. 谢谢。

Try: fancyAlert.TNSFancyAlert.showSuccess() . 试试: 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. [编辑]这确实应该起作用,因为它反映了Narendra Mongiya在您的问题中所链接的问题文件中给出的解决方案。 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. 要记住的一件事是它是一个Promise,所以让我们在其中添加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);
});

You may also find that you're simply calling it from the wrong place in your code. 您可能还会发现只是在错误的代码位置调用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 D3.js 7.0.0 与 Next.js 11.0.1 一起使用时,如何解决“[ERR_REQUIRE_ESM]:必须使用导入加载 ES 模块”的问题? - How do I resolve "[ERR_REQUIRE_ESM]: Must use import to load ES Module" when using D3.js 7.0.0 with Next.js 11.0.1? 如何在不使用“需要”或“导入”的情况下导入NPM模块? - How do I import an NPM module without using 'require' or 'import'? 如何修复 SyntaxError:无法在使用 Jest 的模块外部使用 import 语句? - How do I fix SyntaxError: Cannot use import statement outside a module using Jest? 如何在Nativescript Vue中导入scss文件? - How do I import scss file in Nativescript Vue? 如何使用异步 ESM 导入获取 ESM 模块 - How do I get a ESM module using the Async ESM import 如何不使用“__dirname”或“import.meta.url”获取Node.js中当前模块的目录? - How do I obtain the directory of the current module in Node.js without using “__dirname” or “import.meta.url”? 使用Node.Js模块,如何在同一模块中使用对象功能? - Using Node.Js Modules, how do I use an object function in the same module? 如何在ASP.NET CORE MVC中导入一个js文件模块? - How to import a js file module in ASP.NET CORE MVC? 如何在 Nuxt.js 中导入 three.js OBJLoader? “不能在模块外使用 import 语句” - How do you import three.js OBJLoader in Nuxt.js? “Cannot use import statement outside a module” 如何在 react.js 中使用节点模块脚本? - how do I use a node module script in react.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM