I have a JavaScript method in an external JS file. In an Ionic 2 project, I have to call some methods of AnyJS function from my home.ts. I have included the JS file inside a <script> tag in index.html. I have declared AnyJS like this declare var AnyJS: any; above @Component in my home.ts.
Now, while creating an object by using the new keyword in the constructor I'm getting the following reference error:
5 713568 error EXCEPTION: Error in ./HomePage class HomePage_Host - inline template:0:0 caused by: AnyJS is not defined 6 713570 error ORIGINAL EXCEPTION: AnyJS is not defined 8 713575 error
ReferenceError: AnyJS is not defined
The Ionic version I'm using:
Cordova CLI: 6.3.1
Ionic Framework Version: 2.0.0-rc.1
Ionic CLI Version: 2.1.0
Ionic App Lib Version: 2.1.0-beta.1
ios-deploy version: 1.8.3
ios-sim version: 5.0.4
OS: Mac OS X El Capitan
Node Version: v6.8.0
Xcode version: Xcode 8.0 Build version 8A218a
Here is the home.ts:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
declare var AnyJS: any;
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
private anyjs : any;
private method : any;
constructor(public navCtrl: NavController) {
this.anyjs = new AnyJS();
this.method = this.anyjs.methodCall();
}
}
And, here is my AnyJS file:
function AnyJS(){}
AnyJS.prototype.methodCall = function() {
console.log("Done!");
return true;
};
Note: AnyJS file contains an object with some properties and methods which doesn't comply with the latest ES6 standards but plain JS methods. I need to create an object and call the methods directly from my home.ts.