1

I am a fresher in IBM MobileFirst 8.0. How to use IBM MFP functions(WL, WLResourceRequest) in ionic 2 projects?

I have created an Ionic Project and added MFP plugin and I added the below lines into Home Component TS file.

WL.Client.setSharedToken({key: "name", value: "Karhik"});
    let str: String = WL.Client.getSharedToken({key: "name"});
    console.log(str);

But I am getting the below error. How to solve the issue. Or given any reference documents for Ionic 2 + IBM MFP 8.0

enter image description here

And I tried to like this below. But same error is returned,

Home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Device } from '@ionic-native/device';

declare var wlCommonInit: () => {

};

declare var WL;

@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
  providers: [ Device ]
})
export class HomePage {

  constructor(public navCtrl: NavController, private device: Device) {
    console.log("device platform " + device.platform);

    WL.Client.setSharedToken({key: "name", value: "Karhik"});
    let str: String = WL.Client.getSharedToken({key: "name"});
    console.log(str);
  }

  wlCommonInit() {
    console.log("wlCommonInit TRIGGERED");

  }

}

But it not working. I need to know how to use IBM MFP 8.0 in IONIC 2

After adding the code into Main.ts Below error is returned in console.

enter image description here

  • 1
    This error happens if you invoke WL objects before MFP plugin initialization is complete. You should wait until MFP bootstrapping is complete , to add any code that initializes MFP objects - "WL" and others. – Vivin K Jul 18 '17 at 12:35
  • Hi, Any event will trigger after MFP bootstrapping is completed? or Any other sample you have please post for my reference. – Karthik Sridharan Jul 18 '17 at 12:53
  • 1
    I would suggest you put your code in wlcommoninit() – Vivin K Jul 18 '17 at 18:21
  • Hi, I am using Ionic 2, I don't know where to add wlcommoninit() method. There doesn't have any main.js. There have main.ts. If i add the wlcommoninit() function in HomeComponent. I getting 'wlcommoninit() is declared but not used' – Karthik Sridharan Jul 19 '17 at 07:57
  • Possible duplicate of [Cannot find referecnce to WLResourceRequest in Ionic 2 App](https://stackoverflow.com/questions/38356571/cannot-find-referecnce-to-wlresourcerequest-in-ionic-2-app) – Prerak Tiwari Aug 17 '17 at 19:35

2 Answers2

2

WL has to be loaded before other javascript files for WL functions to be available in these files. The initialization of WL happens in wlcommoninit() (it's usage can be found here). The recommended approach is to ensure that the other js files are loaded in this function.

An alternative to overcome this issue is to ensure that the below 3 scripts are included before the other js scripts in the html.

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/platform.js"></script>
<script type="text/javascript" src="js/main.js"></script>
Keerthi
  • 423
  • 3
  • 9
1

Add the following line before the import statements instead of declaring WL and wlCommonInit.

/// <reference path="../../../plugins/cordova-plugin-mfp/typings/worklight.d.ts" />

This should resolve the issue which you are facing.

Vittal Pai
  • 3,317
  • 25
  • 36