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 ?
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:
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.
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.
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.
This solution only worked for me
Put the import js in
src/index.html
header tag, before thebuild/polyfills.js
andbuild/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); ...
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With