简体   繁体   中英

Typescript compiler/Cordova API: Check for undefined global variables

I'm new to Typescript, or better in Angular 2 with Typescript.

The problem is that I cannot compile my code due to undefined Cordova API global variables in my local dev environment. These variables will be obviously defined once the app will be compiled and packed for production, so with Cordova libraries.

For example, with Angular 1 and Javascript I could have this:

if (typeof(device) != 'undefined') {
    return device.platform;
}

Now with Angular 2 and TypeScript compiler tells me that it cannot find name device.

How to work so with these unavailable variables in dev environment? (I just build and test my app on the browser first)

Thanks

It looks like this is in the Ionic 2 docs for device plugin . What you need to do is import the Device class from ionic-native and call the uuid property of the device object.

For example

import {Device} from 'ionic-native';

platform.ready().then(() => {
   console.log(Device.device.uuid);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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