简体   繁体   English

Ionic本机设备插件@ ionic-native / device返回所有null

[英]Ionic native device plugin @ionic-native/device returns all nulls

I need to do a device detection in my Ionic project so I've installed @ionic-native/device plugin per instructions here: https://ionicframework.com/docs/native/device/ 我需要在Ionic项目中进行设备检测,因此我已经按照此处的说明安装了@ionic-native/device插件: https : //ionicframework.com/docs/native/device/

However when I wire it in inside of a component, then run ionic serve to preview changes, console.log returns Device object with all values set to null , same happens when I try to use individual property eg this.device.model : 但是,当我将其连接到组件内部,然后运行ionic serve来预览更改时,console.log返回所有值都设置为null Device对象,当我尝试使用单个属性(例如this.device.model发生同样的情况: console.log输出

Here is how I use it inside of a component: 这是我在组件内部使用它的方式:

import {Device} from "@ionic-native/device";
// ...

@Component({
    // ...
})
export class MyComponent {

    constructor(private device: Device) {
    }

    ngOnInit() {
        console.log(this.device);
    }

}

And I've added it to AppModule as well: 我也将其添加到AppModule中:

import {Device} from "@ionic-native/device";
// ...

@NgModule({
    // ...
    providers: [
        Device
    ]
})
export class AppModule() {
}

Cordova device plugin was auto injected into config.xml : Cordova设备插件已自动注入config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget <!-- ... --> >
    <!-- ... -->
    <plugin name="cordova-plugin-device" spec="2.0.0" />
</widget>

Here is my Ionic stack (at least packages that should be relevant to the issue): 这是我的Ionic堆栈(至少与该问题相关的软件包):

"@angular/*": "^5.2.4",      // all packages
"@ionic-native/*": "4.5.2",  // all packages

"@ionic-native/device": "4.5.2"
"ionic-angular": "3.9.2",
"cordova-plugin-device": "2.0.0",
"typescript": "2.6.2"

Thanks! 谢谢!

UPDATE: 更新:

I was able to get device details in the browser by running: 通过运行以下命令,我可以在浏览器中获取设备详细信息:

cordova run browser

This assumes you have added browser as a platform, if not run: 假设您已将浏览器添加为平台(如果未运行):

ionic cordova platform add browser

(From the link in the answer posted by @AndrewLively: https://stackoverflow.com/a/49034015/448816 ) (从@AndrewLively发布的答案中的链接: https ://stackoverflow.com/a/49034015/448816)

If you are running in the browser using ionic serve then most of the ionic-native plugins won't work since it is not treated by ionic as a valid browser platform. 如果您在使用ionic serve的浏览器中运行,那么大多数ionic-native插件将无法工作,因为ionic并未将其视为有效的浏览器平台。

This is not well documented, but is discussed in an issue in the ionic-native repo . 这没有很好的记录,但是在离子本征回购中的一个问题中进行了讨论。

Should see the packages installed and see they are in the same version in "package.json" 应该会看到已安装的软件包,并在“ package.json”中看到它们具有相同的版本

在此处输入图片说明

Core and Native packages must be in the same version or the native package should not be larger. 核心软件包和本机软件包必须具有相同的版本,否则本机软件包不能更大。

在此处输入图片说明

The folder "platforms" must be delete 文件夹“平台”必须删除

在此处输入图片说明

Use these commands 使用这些命令

    ionic build

    ionic cordova platform add browser

    cordova run browser

code for .ts .ts的代码

console.log('Device Model is: ' + this.device.model);

And works ! 和工程!

在此处输入图片说明

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

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