How to include an external Javascript library in an Ionic 2 TypeScript project?

I will build an Ionic project using Ionic 2, Angular 2, and TypeScript to test the structure a bit. I need to include an external library ( ntc.js ) in my project, since I need it to call hex colors.

I know that including the Javascript library for TypeScript should work, since everything that works in JS works in TS. I just don't want to include it in the wrong way.

I tried adding the library to www / build / js but it doesn't seem to work and it doesn't seem like a good way to do this. I tried to find ways to do this, but found nothing (maybe because Angular 2 and Ionic 2 are still fresh).

Things like:

import * as ntc from '../../js/ntc';

doesn't seem to work even if my library is in the right place. TypeScript doesn't seem to read my file properly if it reads it at all.

What is a good way to do this? Where should I put my .js file in the project directory?

+4
source share
2 answers

You import it by adding it to your index.html, like any other regular javascript file.

Then in your ts file you do:

declare var Tree:any; 

Then in your code, you can use the Tree variable, although it exists in the Javascript file. this line of code basically tells the typescript compiler, there is a variable, there is a Tree that it should ignore.

+8
source

var, ts, , typescript.

typings install libraryname

, var/class, / .

import {lib} from 'libraryname';
0

Source: https://habr.com/ru/post/1654473/


All Articles