I got a Ionic layout from a coworker. I’m not 100% familiar with typescript and angular v2. I can work in her layout but I fail at just including an local JS file (i.e require("./functions.js"))
So the main goal is: include a local (read: in the src-folder) JS file in a .ts file (i.e a component) without setting of any linting alarms.
I tried a number of things, including:
Adding "allowJs": true, to tsconfig.json
Fiddling around with the .ts file where I require the file (I keep getting linting errors I can’t seem to ignore).
Doing a Google search returns out nothing. Please help, I took more time solving this than actually building the functions themselves
Thanks, but unfortunately I couldn’t find a solution to my issue that easily. Let me explain:
Suppose I have this class:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@IonicPage()
@Component({
selector: 'page-commonbike',
templateUrl: 'commonbike.html'
})
export class CommonBikePage {
constructor(public navCtrl: NavController) {
}
}
What I want to do is to require() a set of functions I made ‘outside’ the ionic project and interact with them. I really don’t know how to get started. What I cam up with is something in the lines of this (which does not compile, maybe I’m not getting the right concept of typescript):
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Functions } from './functions.js';
@IonicPage()
@Component({
selector: 'page-commonbike',
templateUrl: 'commonbike.html'
})
export class CommonBikePage {
functions: object;
constructor(public navCtrl: NavController) {
var f = new Functions()
}
}
So all in all, I don’t want to deal with typescript at all in regards to my contributions to our project (functions.js). It is fine that the ionic part is using it. So I’m trying to make a js-only interface that allows me to add anything without errors to the ionic-part.
My Js file exposes a JS object called CryptoJS which has some properties and functions as shown above. Just declare this somewhere in TypeScript (If its just a function then something like declare let yourFunction: (params) => string or even just declare let yourFunction: any could just be enough).
Be aware that this declaration contains no implementation of the methods, just a hint for TS “hey here is an object called X with following properties/methods”.
Thanks, unfortunately I can’t ‘include the JS file in your index.html file.’ (as in using script tags) becuase the file has imports itself. The only possible way would be to compile functions.js with webpack and then import the result using a script tag, I hope that’s won’t be necessary
I also have these very weird errors when including the JS files:
[12:29:14] typescript error
Cannot write file '/home/peter/Projects/app/src/app/functions.js' because it would overwrite
input file.
[12:29:14] typescript error
Cannot write file '/home/peter/Projects/app/src/utils/storagehelpers.js' because it would
overwrite input file.
When googling about this error often answers tell you to remove allowJS but that’s what I actually want.
My TSconfig is like this:
Running into exactly the same issue and coming across same suggestions like using script tag (which I can’t use for same reason as yours) and allowJs which then gave same problem: Cannot write file.
Using a js node-module in ionic ts works, but directly importing a js file which is not in node-modules doesn’t work.
I think it is because node-modules are compiled separately in the vendor.js (which seemingly supports js imports) and app’s src/ ts files are compiled separately using different typescript config etc… (which seemingly doesn’t allow js imports).
What is confusing is that typescript ‘allowJs’ is probably the official way to resolve this, but it doesn’t work due to other ionic build steps, either ‘Cannot write file’ error or if that is resolved using the outDir option then ‘Template not found’ error.
-> Using a js node-module in ionic ts works, but directly importing a js file which is not in node-modules doesn’t work.
This worked for me:
I made a local copy of moment.js then imported it:
import moment from 'moment';
...
public ngOnInit() {
this.logger.info('IntroductionPage: ngOnInit()');
let data = moment().format('YYYYMMDD');
let time = moment().format('HHmmss');
this.logger.info('today is: ', data + ' and time: ', time);
}