0

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.

1 Answer 1

2

Change your AnyJs file to something like this:

export class AnyJs {

  public methodCall () {
    console.log('done');
    return true;
  }

}

Then in your home.ts file import it:

import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';

import { AnyJs } from './anyjs.ts'; // or the path of the file

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

export class HomePage {

    constructor(
      public navCtrl: NavController,
      public anyJs: AnyJs) {
        this.anyJs = anyJs; 
        // And then you can use it anywhere in your file like:
        this.anyJs.methodCall().success(data => console.log(data));
    }

}

Now if your AnyJs file is an ES6 file you have those options:

Declare a module and export the class to use:

declare module "foo" { export class Foo{ } }

Or Declare function as a module

export default function () { ... };


import myFunc from 'AnyJs';
myFunc();
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your help. But, do I need to rewrite the functions in the external js in TypeScript? Is there any other solution for importing my js file as it is without rewriting in TypeScript?
You can declare a module with es6: declare module "foo" { export class Foo{ } } for example.
Could be please explain this as well? Because I want to import my js file as it is without rewriting in TypeScript.
Thanks. I updated my question. So, basically I want to call the methods of AnyJS file directly from my home.ts. Is there any other alternative? If not, what would be the best approach?
Still getting the error: Cannot find name 'AnyJs'. constructor(public navCtrl: NavController, public anyJs: AnyJs) {
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.