最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I import Math module using Nodejs? - Stack Overflow

programmeradmin2浏览0评论

I'm using NativeScript framework to build my app. However, I can't use any Math function. I guess it's because NativeScript work with NodeJS (and I never used Node before, and I don't know exactly how to use it), so you have to require some modules. I'm gonna show my XML example here:

<Page>
<StackLayout>
    <Button text="See the alert" tap="sqrt" />
    <Button text="Return to Menu" tap="return" />
</StackLayout>
</Page>

Its really simple. I have two different buttons in this screen. The first one should use the sqrt function (and that's the one I'm having trouble with) and the second button should return to the menu page in my app (and this one works fine). Both XML and JS files has the same name and, considering that I'm using NativeScript framework, it happens that XML and JS are executed at the same time because of that. Here I show the JS file:

var frameModule = require("ui/frame");

exports.return = function() {
    var dummyvariable = frameModule.topmost();
    dummyvariable.navigate("app/menu/menu");
};

exports.sqrt = function() {
    var x = Math.sqrt(4);
    alert(x);
};

The first function works normally when I tap the "Return to Menu" button. In return function, I required a module called frameModule to change the screen I was looking. However, it appears in my app that "Math.sqrt" is not a function when I tap the "See the alert" button. Do you have any idea about how to solve this little but tricky problem?

Thanks!

I'm using NativeScript framework to build my app. However, I can't use any Math function. I guess it's because NativeScript work with NodeJS (and I never used Node before, and I don't know exactly how to use it), so you have to require some modules. I'm gonna show my XML example here:

<Page>
<StackLayout>
    <Button text="See the alert" tap="sqrt" />
    <Button text="Return to Menu" tap="return" />
</StackLayout>
</Page>

Its really simple. I have two different buttons in this screen. The first one should use the sqrt function (and that's the one I'm having trouble with) and the second button should return to the menu page in my app (and this one works fine). Both XML and JS files has the same name and, considering that I'm using NativeScript framework, it happens that XML and JS are executed at the same time because of that. Here I show the JS file:

var frameModule = require("ui/frame");

exports.return = function() {
    var dummyvariable = frameModule.topmost();
    dummyvariable.navigate("app/menu/menu");
};

exports.sqrt = function() {
    var x = Math.sqrt(4);
    alert(x);
};

The first function works normally when I tap the "Return to Menu" button. In return function, I required a module called frameModule to change the screen I was looking. However, it appears in my app that "Math.sqrt" is not a function when I tap the "See the alert" button. Do you have any idea about how to solve this little but tricky problem?

Thanks!

Share Improve this question asked May 16, 2018 at 17:41 Patrick RoncoskiPatrick Roncoski 111 gold badge1 silver badge2 bronze badges 2
  • Not familiar with NS but this came up in a quick search. – Phix Commented May 16, 2018 at 17:59
  • Have you tried importing a Node module to solve the problem? mathjs – TinkerTenorSoftwareGuy Commented May 16, 2018 at 18:55
Add a ment  | 

1 Answer 1

Reset to default 3

My environment is slightly different from yours, mine is a pure server application.

sudo npm install mathjs -g

where mathjs was installed under /usr/local/lib/node_modules/ in my system. Now I can import mathjs with the following code:

math = require("/usr/local/lib/node_modules/mathjs")
math.pow(2, 3)  # 8
math.exp(-3)    # 0.049787068367863944

If mathjs was installed locally using "npm install mathjs", you can import mathjs:

math = require("mathjs")

Environment: Ubuntu 18.04.2, nodejs 8.10.0, npm 3.5.2

P.S. From a browser, you may import math.js using the following code according to math.js docs (Note: I did not test it):

<html>
<head>
  <script src="math.js" type="text/javascript"></script>
</head>
</html>
发布评论

评论列表(0)

  1. 暂无评论