3rd Party Library “cordova-plugin-ms-azure-mobile-apps” not getting recognized in my Ionic 2 app

自闭症网瘾萝莉.ら 提交于 2019-12-06 09:51:54

Finally, got it working! Thanks for Microsoft support!

Not the best solution, I think, in the world of TypeScript but that's the only way to make it work.

So, all I had to do was to use the "WindowsAzure" namespace from "cordova-plugin-ms-azure-mobile-apps" as a regular plain old JavaScript variable instead of doing an "import".

That is, instead of importing WindowsAzure as:

import { WindowsAzure } from 'cordova-plugin-ms-azure-mobile-apps';

I declared it as a regular JavaScript object after all the "imports" and before "@Component" class and/or "@Injectable" service/provider class, as:

```

import { ... } from '...';

declare var WindowsAzure: any;

@Injectable
export class ... {
    client: any;
    ....
    constructor or yourMethod(...) {
        this.client = new WindowsAzure.MobileServiceClient('http://YourAzureMobileApp.azurewebsites.net');
    }
    ....
    ....
    //Your logic
}

```

So, apparently, in Ionic 2 if your third party library is not being recognized with regular "import" statement then you should try to use it in the old JavaScript way.

It was that simple a solution on something where I ended up spending 2 days. :(

Hope this helps somebody save their time.

Suraj Rao

To import a 3rd party cordova plugin which is not included in Ionic Native.

Try this: Check answer here.

If declaring cordova does not work: Do

ionic plugin add cordova-plugin-ms-azure-mobile-apps --save

IT should and is installing the plugin correctly.

Check the plugin.xml file in

plugins/cordova-plugin-ms-azure-mobile-apps There should be an entry:

   <js-module src="src_of_js" name="some_name">
    <clobbers target="global_object_name" />   </js-module>

Generally the clobbers target name is the global object name that is used for the plugin when all the cordova plugins are loaded.(This is generally cordova.plugins.somename) Typescript generally complains as it cannot find the global variable at transpilation time.

Just do:

declare var global_object:any;

after all the other imports in your file and use the plugin.

Whenever you add 3rd party cordova plugin, usually it adds the new objects to window object. And you don't need to import anything to use those objects. And It is also true for WindowsAzure plugin.

Once you add the 'cordova-plugin-ms-azure-mobile-apps' in your project, you can use window.WindowsAzure to call its functions.

Note: If the editor or build process complaints about window variable then you can define it in your ts file just after import statements as followed:

declare var window: any;

Here are the steps to add and use the cordova-plugin-ms-azure-mobile-apps plugin in an ionic app.

# Create a hello world ionic v2 project
ionic start ionicv2 --v2
cd ionicv2

# Add the Cordova plugin
ionic plugin add cordova-plugin-ms-azure-mobile-apps

# Add the following 3 lines to app.component.ts
declare var WindowsAzure: any;
var client = new WindowsAzure.MobileServiceClient("https://yoursitename.azurewebsites.net");
window.alert("MobileServiceClient instance: " + client);

# Build and run for browser
ionic platform add browser
ionic run browser
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!