Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Include JS file in ionic 3

Tags:

Im looking for a way to access a variable from external js file which i included in assets/data folder

below is what i tried

placed test.js in assets/data folder

in test.js added variable testvar = "heloo from external js";

added script tag in src/index.html <script src="assets/data/test.js"></script>

in app.component.ts i added this line after imports;declare var testvar: any;

in constructor added this line to log the value console.log(testvar);

result is error : ERROR ReferenceError: testvar is not defined

so, how can i use my js variable in typescript ?

like image 314
rashidnk Avatar asked May 07 '17 16:05

rashidnk


People also ask

How to add CSS and JS to your ionic app?

Afterwards you can load CSS or JS files directly during the build process of your app. 1. Install dependencies Of course you start by installing a NPM package to your Ionic app. In this case, I used the css-animator package to easily add animations to my app:

How do I add scripts to my Ionic 2 project?

I know this is old, but for those who are struggling with this for Ionic 2, add script files to src/assets/scripts, then refer to them via a script tag in src/index.html (not the one in www). When it builds, everything in src/assets will be copied to www/assets, including your scripts. index.html will also be copied to www.

How many lines of JavaScript are there in an ionic app?

Looking back, there is quite a bit of functionality enabled by 12 lines of HTML in the template and about 20 lines of JavaScript in the controller. We have taken a look at a number of components, which you will frequently use in your Ionic apps. Ionic JavaScript components are used as HTML elements and can work in a coordinated manner.

How do I use external libraries and npm packages in ionic?

When working with external libraries and NPM packages, you sometimes need to load those files in a different way than the rest of your packages is already loaded. In this Quick Win you will see how to easily install and integrate those plugins so you can use them just like any other library in your general Ionic workflow.


2 Answers

This solution only worked for me

Put the import js in src/index.html header tag, before the build/polyfills.js and build/main.js (they are in body tag);

Example : I created a file src/assets/test.js with a var testvar, imported in src/index.html and then in src/app/app.component.ts declared declare var testvar;.

test.js

var testvar = "Hello from external js"; 

index.html

...   <link rel="icon" type="image/x-icon" href="assets/icon/favicon.ico">   <link rel="manifest" href="manifest.json">   <meta name="theme-color" content="#4e8ef7">    <!-- cordova.js required for cordova apps -->   <script src="cordova.js"></script>   <script src="assets/js/test.js"></script> //here, not in body ... 

app.componet.ts

declare var testvar;  @Component({    templateUrl: 'app.html' }) export class MyApp {   @ViewChild(Nav) nav: Nav;   constructor(private statusbar : StatusBar,  splashScreen: SplashScreen) {    alert(testvar); ... 
like image 128
rashidnk Avatar answered Nov 01 '22 12:11

rashidnk


To expand on misha130's answer. You would need to import it into the file you want like this:

import * as test from '../assets/data/test' 

This way you have access to the test variable.

console.log(test.testvar); 
like image 35
Logan Garland Avatar answered Nov 01 '22 12:11

Logan Garland