简体   繁体   English

Javascript库在设备上不起作用

[英]Javascript library not working on Device

I am using the BigNumber library of MikeMcl to handle my needs for big numbers. 我正在使用MikeMcl的BigNumber库来处理我对大数的需求。 I use this library in an Ionic/Angular project . 我在Ionic / Angular项目中使用此库。

As explained on Github , the way to install and use this library is to include a script tage in your html: Github所述,安装和使用此库的方法是在html中包含脚本时代:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <!-- compiled css output -->
    <link href="css/ionic.app.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.min.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
    <script src="cordova.js"></script>

    <!-- ********* BIGNUMBER library ********* -->
    <script src='lib/bignumber.js/bignumber.min.js'></script>    

    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>

  </head>
  <body ng-app="starter">
    <ion-nav-view></ion-nav-view>
  </body>
</html>

Now in my code, I can use this library as for instance: 现在在我的代码中,我可以使用该库,例如:

x = new BigNumber(123.4567)
y = BigNumber('123456.7e-3')
z = new BigNumber(x)
x.equals(y) && y.equals(z) && x.equals(z)      // true

I tested this and it works fine on Chrome and on Safari (even on device). 我对此进行了测试,它在Chrome和Safari(甚至在设备上)上也能正常运行。

But when I install the app on my phone, using Phonegap Build , the app does not work anymore (I checked that this library is the cause by removing the BigNumber syntaxes from my code). 但是,当我使用Phonegap Build将应用程序安装在手机上时,该应用程序将无法再运行(我从代码中删除了BigNumber语法,从而检查了该库是否是该原因)。 Moreover, the Angular in the app just crashes and shows for instance {{variableName}} and nothing is working. 此外,应用程序中的Angular崩溃了,并显示了例如{{variableName}} ,但没有任何作用。

Because I develop in a cloud environment, I did try to debug the app using the Safari Developer Debug Console on a mac (by plugging my phone with an USB to the mac and then enabling Developer tools in Safari). 因为我是在云环境中进行开发的,所以我确实尝试在Mac上使用Safari开发人员调试控制台调试应用程序(通过将手机的USB插入Mac,然后在Safari中启用开发人员工具)。

However, no errors were found with exception of one: 但是,除以下一项外,未发现任何错误:

file not found: "path/to/ionic/lib/js/angular.min.js.map" 404

But this is not the cause of the problem . 但这不是问题的原因

What is going on? 到底是怎么回事? How can I still use this javascript library on my device? 我如何仍可以在设备上使用此javascript库?

I have just tested your code and it doesn't work because when you create the y variable you don't use the word new. 我刚刚测试了您的代码,但它不起作用,因为在创建y变量时,您不会使用单词new。

This is the code I've used and it works 这是我使用过的代码,可以正常工作

x = new BigNumber(123.4567)
y = new BigNumber('123456.7e-3')
z = new BigNumber(x)
alert( x.equals(y) && y.equals(z) && x.equals(z));// I get true

@JohnAndrews, @JohnAndrews,

If this is the extent of your code, it is missing the deviceready listener. 如果这是您代码的范围,则它缺少设备就绪侦听器。 On cordova/phonegap you typically cannot do stuff, until you get the deviceready event. 在cordova / phonegap上,您通常无法做任何事情,直到收到deviceready事件。 So, this is the only thing I can think of, Can you add a function onDeviceReady() {} and see if that fixes your problem? 因此,这是我唯一能想到的事情,是否可以在function onDeviceReady() {}添加一个function onDeviceReady() {} ,看看是否可以解决您的问题?

Oh and, of course, make sure you run your operations AFTER this event. 哦,当然,请确保您在此事件之后运行操作。

Found the solution. 找到了解决方案。 If you are also experiencing that a javascript library is not working on your device, but it does in the browser, then this answer might help you. 如果您还遇到JavaScript库无法在您的设备上运行但在浏览器中运行的情况,那么此答案可能会对您有所帮助。

The problem occured that when I ran npm install to install the package, that it did not update my .git dependencies properly. 发生问题是,当我运行npm install来安装软件包时,它没有正确更新我的.git依赖项。 What happened is that every time I packaged my app through Github and Phonegap Build, the folder of BigNumber was not taken into account. 发生的事情是,每次我通过Github和Phonegap Build打包应用程序时,都不会考虑BigNumber文件夹。 So basically, the file bignumber.js was not available in the app and thats why it did not work. 因此,基本上,文件bignumber.js在应用程序中不可用,这就是为什么它不起作用的原因。

I solved it by copying all the files from the BigNumber folder to a new custom folder. 我通过将所有文件从BigNumber文件夹复制到新的自定义文件夹来解决了该问题。

A related question is here: Git not pushing all files and folders 一个相关的问题在这里: Git没有推送所有文件和文件夹

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM