Home Web Front-end JS Tutorial How to call third-party libraries in Angular

How to call third-party libraries in Angular

Jun 07, 2018 pm 02:11 PM
angular Third-party libraries

This time I will show you how to call third-party libraries in Angular, and what are the precautions for calling third-party libraries in Angular. The following is a practical case, let's take a look.

Angular’s ​​components and modules seem to be a bit incompatible with existing third-party libraries (such as lodash, moment, etc.). The main reason for this is the illusion caused by TypeScript. The three pillars of front-end are actually the same, no matter what kind of front-end framework, these third-party libraries can be used.

Below I will explain from another perspective how Angular uses third-party libraries as an experience.

1. Written in front

Before you start, you need to understand the TypeScript module system - a module refers to a module in its own scope Executed in the global scope instead of in the global scope; the relationship between modules relies on export and import. During the compilation process, the compiler also relies on this relationship to locate the files that need to be compiled.

TypeScript still publishes class libraries in the form of JavaScript files, which results in types that cannot be expressed and require a declaration file to describe the type; therefore, the declaration file has become an indispensable part of the class library.

2. Classification

Angular is developed using TypeScript language. As mentioned in the previous section, if you want a class library to be used, The requirement is whether there is a declaration document.

There is a declaration file

To distinguish whether the class library has a declaration file *.d.ts, you can confirm this from two aspects:

1. The class library comes with

After installing a dependent package from Npm, you can directly check whether the package.json of its library contains the typings node, for example, moment:

"typings": "./moment.d.ts"
Copy after login

2. TypeSearch retrieval

TypeScript provides a website called TypeSearch, where you can directly enter keywords to check whether the declaration file of the class library is included.

For example, lodash can be clicked in the list to jump to the npm website, and you will see a command like this:

npm install --save @types/lodash
Copy after login

No declaration file

This kind of situation is quite common. For example, G2 did not have a declaration file earlier. In this case, you can only write the declaration file yourself.

The project created by Angular Cli will contain a src/typings.d.ts declaration file, which will be automatically included in the global declaration definition, and it is best to write the declaration information of these class libraries in it.

Generally speaking, it is difficult to write a complete declaration file for a class library. This is too cost-effective, so we often only make an "any" for some global objects (meaning to ignore the static type). Check) can also be used, for example:

// src/typings.d.ts
declare var G2: any;
Copy after login

3. How to use?

The declaration file is a link, and it is still used in this way to divide how to use it.

For declaration files, there is no need to do anything extra, just use import where the module is needed, for example:

import * as moment from 'moment';
moment(); // 当前时间
Copy after login

No declaration file

It is important to look at what to do when there is no declaration file. As mentioned earlier, using any to indicate ignoring static type checking means that the user cannot enjoy the pleasure of intelligent prompts brought by the declaration file.

Like G2, we can use it directly anywhere in the project, but it can only recognize G2 variables, and the methods or properties of the instance are agnostic.

// app.component.ts
const g2 = new G2();
g2. // 输入 `.` 后是不会有任何方法或属性
Copy after login

In addition, TypeScript will not perform any type check on G2 during the compilation process. Whether G2 really exists can only be determined by yourself. For Angular, these modules need to be explicitly loaded in the scripts node of .angular-cli.json.

// .angular-cli.json
"scripts": [
  "../node_modules/@antv/g2/dist/g2.min.js"
]
Copy after login

TypeScript is still JavaScript code after compilation. If you do not manually load G2 related JavaScript files, it will naturally provide an error that G2 is not found during the running process.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Summary of how to use state objects in vuex

##actual usage analysis of react-redux plug-in project

The above is the detailed content of How to call third-party libraries in Angular. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1234
24
How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

An article exploring server-side rendering (SSR) in Angular An article exploring server-side rendering (SSR) in Angular Dec 27, 2022 pm 07:24 PM

Do you know Angular Universal? It can help the website provide better SEO support!

A brief analysis of how to use monaco-editor in angular A brief analysis of how to use monaco-editor in angular Oct 17, 2022 pm 08:04 PM

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

Detailed explanation of angular learning state manager NgRx Detailed explanation of angular learning state manager NgRx May 25, 2022 am 11:01 AM

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

How to use PHP and Angular for front-end development How to use PHP and Angular for front-end development May 11, 2023 pm 04:04 PM

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

Token-based authentication with Angular and Node Token-based authentication with Angular and Node Sep 01, 2023 pm 02:01 PM

Authentication is one of the most important parts of any web application. This tutorial discusses token-based authentication systems and how they differ from traditional login systems. By the end of this tutorial, you will see a fully working demo written in Angular and Node.js. Traditional Authentication Systems Before moving on to token-based authentication systems, let’s take a look at traditional authentication systems. The user provides their username and password in the login form and clicks Login. After making the request, authenticate the user on the backend by querying the database. If the request is valid, a session is created using the user information obtained from the database, and the session information is returned in the response header so that the session ID is stored in the browser. Provides access to applications subject to

A brief analysis of independent components in Angular and see how to use them A brief analysis of independent components in Angular and see how to use them Jun 23, 2022 pm 03:49 PM

This article will take you through the independent components in Angular, how to create an independent component in Angular, and how to import existing modules into the independent component. I hope it will be helpful to you!

What should I do if the project is too big? How to split Angular projects reasonably? What should I do if the project is too big? How to split Angular projects reasonably? Jul 26, 2022 pm 07:18 PM

The Angular project is too large, how to split it reasonably? The following article will introduce to you how to reasonably split Angular projects. I hope it will be helpful to you!

See all articles