简体   繁体   English

科尔多瓦设备插件不工作

[英]Cordova device plugin not working

I have a simple Phonegap Build application where I am trying to use the device plugin.我有一个简单的 Phonegap Build 应用程序,我正在尝试使用设备插件。 I have installed the plugin via command line and have confirmed that it is installed.我已经通过命令行安装了插件并确认它已安装。

I have the following js inside onDeviceReady:我在 onDeviceReady 中有以下 js:

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

I get white screen with no alerts.我得到没有警报的白屏。

Chrome dev tools remote inspect console says: Uncaught ReferenceError: platform is not defined Chrome 开发工具远程检查控制台说:Uncaught ReferenceError: platform is not defined

The plugin is not being recognized.插件未被识别。

Add this cordova pluign using this commend:使用此命令添加此 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>

Try this试试这个

<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>

If you are using phonegap build you don't have to install the plugins with the CLI, you have to add it in the config.xml file that you upload to phonegap build如果您使用的是 phonegap build,则不必使用 CLI 安装插件,您必须将其添加到上传到 phonegap build 的 config.xml 文件中

Add this line:添加此行:

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

And don't use it before the deviceready event is fired.并且在deviceready事件被触发之前不要使用它。

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

I hope you can solve this issue by using this cordova device plugin .Its an updated as well as live plugin with properties including model,platform,uuid,version,manufacturer,serial number etc...我希望你能通过使用这个cordova 设备插件来解决这个问题。它是一个更新的实时插件,具有模型、平台、uuid、版本、制造商、序列号等属性……

you can add this plugin to your project via cli using the command您可以使用以下命令通过 cli 将此插件添加到您的项目中

cordova plugin add cordova-plugin-device

if you have added any previous device plugins, please remove that prior to adding this plugin.如果您添加了任何以前的设备插件,请在添加此插件之前将其删除。

I was stumped with this same problem for awhile.有一段时间我被同样的问题难住了。 First I built to iOS and everything was working great, then I went to add the Android platform to make the app for Android and began getting the error.首先我构建了 iOS 并且一切正常,然后我去添加 Android 平台来为 Android 制作应用程序并开始出现错误。

Re-adding the plugin to my project after creating the Android platform fixed the error for me.在创建 Android 平台后将插件重新添加到我的项目中为我修复了错误。 I'm sure there have to be others who have had the same issue, so I wanted to share.我敢肯定肯定还有其他人遇到过同样的问题,所以我想分享一下。

cordova plugin add cordova-plugin-device

If this is Angular/Ionic, you need to do two things:如果这是 Angular/Ionic,你需要做两件事:

  1. import { Device } from '@ionic-native/device/ngx' <- this is how the import should look like import { Device } from '@ionic-native/device/ngx' <- 这就是导入的样子
  2. Do the same import on the module that contains the components you gonna use the Device class, and add the cass to that module's provider.对包含您要使用 Device 类的组件的模块执行相同的导入,并将 cass 添加到该模块的提供程序。

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

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