Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling a variable from a javascript file returns an undefined: Ionic

I’m using the Ionic framework and I want to call a variable from a javascript file, but it returns undefined for the variable and prints the console inside the file.

javascript file:

console.log('Hi!')
var test = "Hello";

typescript file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import * as testfile from "src/assets/js/customers"

export class CustomersPage implements OnInit {
  test:any='j';
constructor (){
this.test= testfile.test;
console.log('Hello from typescript', this.test);
}

}

The Result

Hi

Hello from typescript undefined

>Solution :

You should export the variable from your JavaScript file for the value to be accessible inside the TypeScript file.

So inside your "src/assets/js/customers" file it should be

export var test = "Hello";

OR

var test = "Hello";
export test;

If this is not a default export you need to import it like

import * as { testfile } from "src/assets/js/customers"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading