Ionic本机设备插件@ ionic-native/device返回所有空值 [英] Ionic native device plugin @ionic-native/device returns all nulls
问题描述
我需要在Ionic项目中进行设备检测,因此我已经按照以下说明安装了 @ ionic-native/device
插件:
这是我在组件内部使用它的方式:
从"@ ionic-native/device"导入{Device};//...@成分({//...})导出类MyComponent {构造函数(私有设备:设备){}ngOnInit(){console.log(this.device);}}
我也将其添加到了 AppModule
中:
从"@ ionic-native/device"导入{Device};//...@NgModule({//...提供者:[设备]})导出类AppModule(){}
Cordova设备插件已自动注入到 config.xml
:
<?xml version ='1.0'encoding ='utf-8'?>< widget<!-...->><!-...->< plugin name ="cordova-plugin-device" spec ="2.0.0"/></widget>
这是我的Ionic堆栈(至少与该问题相关的软件包):
"@ angular/*":"^ 5.2.4",//所有包"@ ionic-native/*":"4.5.2",//所有软件包"@ ionic-native/device":"4.5.2"离子角":"3.9.2","cordova-plugin-device":"2.0.0","typescript":"2.6.2"
谢谢!
更新:
我能够通过运行以下命令在浏览器中获取设备详细信息:
cordova运行浏览器
这假设您已将浏览器添加为平台(如果未运行):
ionic cordova平台添加浏览器
(通过@AndrewLively发布的答案中的链接: https://stackoverflow.com/a/49034015/448816)
如果您使用 ionic serve
在浏览器中运行,则大多数ionic-native插件将无法正常工作被ionic视为有效的浏览器平台.
此文档没有很好的记录,但是在离子-天然回购中的问题.
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/
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 e.g. this.device.model
:
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:
import {Device} from "@ionic-native/device";
// ...
@NgModule({
// ...
providers: [
Device
]
})
export class AppModule() {
}
Cordova device plugin was auto injected into 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):
"@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)
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.
This is not well documented, but is discussed in an issue in the ionic-native repo.
这篇关于Ionic本机设备插件@ ionic-native/device返回所有空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!