How to use / select 2 types in an Angular 2 project?

I am developing an Angular 2 project with Typescript. I want to be able to use select2, and I don't see any stable feature rich plugins equivalent to select2. However, I came across a types/select2in the DefinitiveTyped project . I am not sure how to use them in my project. I also cannot find examples of other DefiniteTyped libraries. I am new to both TS and Angular 2. Can anyone give a hint on how to use this?

I understand that I can enable the library using my package.json, but I don’t know how to use it in the component.

+5
source share
3 answers

With @ types / select2 installed, it allows the typescript compiler to do static type checking to make sure that the Select2 library is called correctly. It does not change the way you use it select2- you still need to install the library select2and use it just as if you weren't checking the type.

For external dependencies (e.g. jQuery or Select2), you must install the appropriate type definition files so that the typescript compiler can resolve external types at compile time. This is preferable to declaring types any(i.e. declare var $: any)

Make sure you also set jQuery types:

npm install @types/jquery

node_modules/@types/jQuery. typescript node_modules/@types.

typescript NodeJS index.d.ts typings package.json. node_modules/@types.

+4

, , :

import 'select2';

:

npm install select2 --save

, jquery, .

0

import { Select2} from 'select2'

:

npm install @types/select2@4.0.48

, Typescript

0

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


All Articles