Dansmith12
Dansmith12

Reputation: 143

Running JavaScript with ionic 2

I have built an algorithm in JavaScript, now I wish to use this algorithm on my ionic 2 application. Ideally, I would like to avoid having to translate the entire algorithm to typescript. So far, i've had some success running javascript in the index.html page but can't seem to be able to CALL those functions from the .ts files.

Can someone please give me some suggestions on ways to integrate my js algorithm in my ionic 2 application, or should i just bite the bullet and use typescript?

Thanks

Upvotes: 2

Views: 2355

Answers (1)

Sandeep Sharma
Sandeep Sharma

Reputation: 1885

You would need (.d.ts) typings definition files for this. For example:

If you have demo.js file with following content.

var setUserInfo = function (firstName, secondName) {
    console.log("demo function called: " + firstName + " " + secondName);
}

module.exports = { setUserInfo: setUserInfo };

You would need to make a declaration demo.d.ts file with following:

declare module User {
    function setUserInfo(firstName: string, secondName: string): void;
}

export = User;

Put above two files in one directory. Now if you want to use js in your ts file then follow below steps:

1). import * as _ from './demo'; // first import file. Here ./demo path is relative to your current directory

2). _.setUserInfo("sandeep", "sharma"); // call method

Hope this will help you !!

Upvotes: 1

Related Questions