Cordova插件不适用于首次构建

时间:2018-01-25 10:09:09

标签: cordova ionic-framework

我正在开发Ionic / Cordova应用程序。还有一些奇怪的原因,当我第一次构建应用程序时。插件无法加载。 但是当我重建应用程序时,它又开始工作了。

这与Phonegap plugins not working on first run中的问题有某种关系,但并不完全相同。

[25/01, 11:18 a.m.] Inder: These are the errors that I get when i build and run the app
[25/01, 11:23 a.m.] Inder: 2018-01-25 11:18:37.640001+0100 Admiral[6647:207621] ERROR: Plugin 'Device' not found, or is not a CDVPlugin. Check your plugin mapping in config.xml.
2018-01-25 11:18:37.640121+0100 Admiral[6647:207621] -[CDVCommandQueue executePending] [Line 159] FAILED pluginJSON = ["Device1680548736","Device","getDeviceInfo",[]]
2018-01-25 11:18:37.640254+0100 Admiral[6647:207621] ERROR: Plugin 'File' not found, or is not a CDVPlugin. Check your plugin mapping in config.xml.
2018-01-25 11:18:37.640362+0100 Admiral[6647:207621] -[CDVCommandQueue executePending] [Line 159] FAILED pluginJSON = ["File1680548737","File","requestAllPaths",[]]
2018-01-25 11:18:37.640482+0100 Admiral[6647:207621] ERROR: Plugin 'NetworkStatus' not found, or is not a CDVPlugin. Check your plugin mapping in config.xml.
2018-01-25 11:18:37.640567+0100 Admiral[6647:207621] -[CDVCommandQueue executePending] [Line 159] FAILED pluginJSON = ["NetworkStatus1680548738","NetworkStatus","getConnectionInfo",[]]

1 个答案:

答案 0 :(得分:0)

看起来您正在尝试访问尚未添加到DOM中的插件。一个简单的解决方案是加载cordova.jsindex.js(其中包含deviceready listener)

<强>唐&#39;吨

<!-- some scripts -->
<script src="scripts/some.js"></script>
<script src="scripts/somthing.js"></script> <!-- This JS trying to access device object created by cordova.js -->


<script src="scripts/cordova.js"></script>
<script src="scripts/index.js"></script> <!-- has deviceready event in it -->
<body>
<!-- content -->

<强>不要

<script src="scripts/cordova.js"></script>
<script src="scripts/index.js"></script> <!-- has deviceready event in it -->

<!-- some scripts -->
<script src="scripts/some.js"></script>     
<script src="scripts/somthing.js"></script> <!-- This JS trying to access device object created by cordova.js -->
<body>
<!-- content -->

最佳和优化的解决方案是在最后加载脚本。但由于某种原因,如果你将它们加载到头部,你需要检查该脚本的顺序