科尔多瓦设备插件不工作

Cordova device plugin not working

我有一个简单的 Phonegap Build 应用程序,我正在尝试使用设备插件。我已经通过命令行安装了插件,并确认它已安装。

我在 onDeviceReady 中有以下 js:

alert(device.platform);alert(device.model);

我得到没有任何警报的白屏。

Chrome 开发工具远程检查控制台说: 未捕获的 ReferenceError:未定义平台

插件未被识别。

试试这个

<html>
  <head>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">
    
    //add event listener
    document.addEventListener("deviceready", onDeviceReady, false);
    
    //device.name -> Gives the name of device.
    //device.cordova -> Gives current version of cordova running on device.
    //device.platrof -> Gives the name of platform.
    //device.uuid -> Gives the UUID.
    //device.version -> Gives the Android Version.

    function onDeviceReady() {
        var vInfo =  'Device Name: '     + device.name     + '\n' +
                            'Device Cordova: '  + device.cordova  + '\n' +
                            'Device Platform: ' + device.platform + '\n' +
                            'Device UUID: '     + device.uuid     + '\n' +
                            'Device Version: '  + device.version;
       alert(vInfo);
    }

    </script>
  </head>
  <body>
  </body>
</html>

使用此命令添加此 cordova 插件:

cordova plugin add cordova-plugin-device

read More

Try this
<html>
  <head>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript">

    //add event listener
    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() { 
       console.log(device); // device object 
       console.log(device.name); // Gives the name of device.
       console.log(device.uuid ); // Gives the UUID.
    }

    </script>
  </head>
  <body>

  </body>
</html>

如果您使用的是 phonegap build,则不必使用 CLI 安装插件,您必须将其添加到上传到 phonegap build

的 config.xml 文件中

添加这一行:

<plugin name="cordova-plugin-device"/>

并且在触发 deviceready 事件之前不要使用它。

http://docs.build.phonegap.com/en_US/configuring_plugins.md.html#Plugins

我希望你能通过使用这个 cordova device plugin 来解决这个问题。它是一个更新的实时插件,具有模型、平台、uuid、版本、制造商、序列号等属性...

您可以使用命令

通过 cli 将此插件添加到您的项目中
cordova plugin add cordova-plugin-device

如果您添加了任何以前的设备插件,请在添加此插件之前将其删除。

有一段时间我被同样的问题难住了。首先,我构建到 iOS 并且一切正常,然后我去添加 Android 平台来为 Android 制作应用程序并开始出现错误。

在创建 Android 平台后将插件重新添加到我的项目中为我修复了错误。我敢肯定肯定还有其他人遇到过同样的问题,所以我想分享一下。

cordova plugin add cordova-plugin-device

如果这是Angular/Ionic,你需要做两件事:

  1. import { Device } from '@ionic-native/device/ngx' <- 这是导入的样子
  2. 对包含您要使用设备的组件的模块执行相同的导入 class,并将 cass 添加到该模块的提供程序。