离子设备插件返回空
ionic device plugin returning null
无论我做什么或尝试我都无法获取有关该设备的信息。我使用的是 ionic 4 的最新版本。
因为网上找的基本上都是
- 不要在浏览器上执行
- 一个一个获取值
当然不是这样。我没有这样做,我有可用的 cordova 等
我正在 android 和 ios 设备上进行测试。我在服务中处理这个问题。我正在用按钮调用函数,所以一切都不仅仅是加载,一切都准备好了。这是我正在努力工作的代码:
import { Injectable } from '@angular/core';
import { Device } from '@ionic-native/device/ngx';
import { Platform } from '@ionic/angular';
@Injectable({
providedIn: 'root'
})
export class InitialService {
eldevice: any = '';
constructor(
private device: Device,
private platform: Platform
) {}
async setup() {
if (this.platform.is('cordova')) { // es movil
this.eldevice = await this.storage.get('device');
if (this.eldevice == null) { // nuevo device
this.eldevice.platform = this.device.platform;
this.eldevice.version = this.device.version;
this.eldevice.uuid = this.device.uuid;
this.eldevice.manufacturer = this.device.manufacturer;
console.log('datos sin await', this.eldevice);
this.eldevice = await this.storage.set('device', this.device);
} else { // device conocido
console.log('datos guardados', this.eldevice);
}
} else { // es virtual
console.log('virtual');
}
}
clearData() {
this.storage.clear();
this.eldevice = null;
}
}
已更新:
任何 cordova 插件都需要您等待平台处于 "ready" 状态。
所以在调用/使用插件之前,你需要调用 platform.ready() 方法并在回调中调用特定的插件函数,或者在 "await".
之后
同时查看您的 setup() 方法,您似乎试图访问在存储检查后获得 'null' 分配给它的对象的属性。请参阅以下代码中的注释:
import { Injectable } from '@angular/core';
import { Device } from '@ionic-native/device/ngx';
import { Platform } from '@ionic/angular';
@Injectable({
providedIn: 'root'
})
export class InitialService {
eldevice: any = '';
constructor(
private device: Device,
private platform: Platform
) {}
async setup() {
// await for platform ready:
await this.platform.ready();
if (this.platform.is('cordova')) { // es movil
this.eldevice = await this.storage.get('device');
if (this.eldevice == null) { // nuevo device
// here below 'eldevice' is null (as confirmed by the check above) and you are attempting to assign something to non existing properties (platform, version etc).
// to fix you need actual object to be assigned to eldevice:
this.eldevice = {
platform: this.device.platform,
version: this.device.version,
uuid: this.device.uuid,
manufacturer: this.device.manufacturer
}
//this.eldevice.platform = this.device.platform;
//this.eldevice.version = this.device.version;
//this.eldevice.uuid = this.device.uuid;
//this.eldevice.manufacturer = this.device.manufacturer;
console.log('datos sin await', this.eldevice);
this.eldevice = await this.storage.set('device', this.device);
} else { // device conocido
console.log('datos guardados', this.eldevice);
}
} else { // es virtual
console.log('virtual');
}
}
clearData() {
this.storage.clear();
this.eldevice = null;
}
}
无论我做什么或尝试我都无法获取有关该设备的信息。我使用的是 ionic 4 的最新版本。
因为网上找的基本上都是
- 不要在浏览器上执行
- 一个一个获取值
当然不是这样。我没有这样做,我有可用的 cordova 等
我正在 android 和 ios 设备上进行测试。我在服务中处理这个问题。我正在用按钮调用函数,所以一切都不仅仅是加载,一切都准备好了。这是我正在努力工作的代码:
import { Injectable } from '@angular/core';
import { Device } from '@ionic-native/device/ngx';
import { Platform } from '@ionic/angular';
@Injectable({
providedIn: 'root'
})
export class InitialService {
eldevice: any = '';
constructor(
private device: Device,
private platform: Platform
) {}
async setup() {
if (this.platform.is('cordova')) { // es movil
this.eldevice = await this.storage.get('device');
if (this.eldevice == null) { // nuevo device
this.eldevice.platform = this.device.platform;
this.eldevice.version = this.device.version;
this.eldevice.uuid = this.device.uuid;
this.eldevice.manufacturer = this.device.manufacturer;
console.log('datos sin await', this.eldevice);
this.eldevice = await this.storage.set('device', this.device);
} else { // device conocido
console.log('datos guardados', this.eldevice);
}
} else { // es virtual
console.log('virtual');
}
}
clearData() {
this.storage.clear();
this.eldevice = null;
}
}
已更新:
任何 cordova 插件都需要您等待平台处于 "ready" 状态。 所以在调用/使用插件之前,你需要调用 platform.ready() 方法并在回调中调用特定的插件函数,或者在 "await".
之后同时查看您的 setup() 方法,您似乎试图访问在存储检查后获得 'null' 分配给它的对象的属性。请参阅以下代码中的注释:
import { Injectable } from '@angular/core';
import { Device } from '@ionic-native/device/ngx';
import { Platform } from '@ionic/angular';
@Injectable({
providedIn: 'root'
})
export class InitialService {
eldevice: any = '';
constructor(
private device: Device,
private platform: Platform
) {}
async setup() {
// await for platform ready:
await this.platform.ready();
if (this.platform.is('cordova')) { // es movil
this.eldevice = await this.storage.get('device');
if (this.eldevice == null) { // nuevo device
// here below 'eldevice' is null (as confirmed by the check above) and you are attempting to assign something to non existing properties (platform, version etc).
// to fix you need actual object to be assigned to eldevice:
this.eldevice = {
platform: this.device.platform,
version: this.device.version,
uuid: this.device.uuid,
manufacturer: this.device.manufacturer
}
//this.eldevice.platform = this.device.platform;
//this.eldevice.version = this.device.version;
//this.eldevice.uuid = this.device.uuid;
//this.eldevice.manufacturer = this.device.manufacturer;
console.log('datos sin await', this.eldevice);
this.eldevice = await this.storage.set('device', this.device);
} else { // device conocido
console.log('datos guardados', this.eldevice);
}
} else { // es virtual
console.log('virtual');
}
}
clearData() {
this.storage.clear();
this.eldevice = null;
}
}