填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

Angular 函式庫開發概述

Overview of Angular libraries

許多應用都需要解決一些同樣的常見問題,例如提供統一的使用者介面、渲染資料,以及允許資料輸入。開發人員可以為特定的領域建立一些通用解決方案,以便在不同的應用中重複使用。像這樣的解決方案就可以建構成 Angular 函式庫,這些函式庫可以作為 npm 套件進行發佈和共享。

Many applications need to solve the same general problems, such as presenting a unified user interface, presenting data, and allowing data entry. Developers can create general solutions for particular domains that can be adapted for re-use in different apps. Such a solution can be built as Angular libraries and these libraries can be published and shared as npm packages.

Angular 函式庫是一個 Angular 專案,它與應用的不同之處在於它本身是不能執行的。必須在某個應用中匯入函式庫並使用它。

An Angular library is an Angular project that differs from an app in that it cannot run on its own. A library must be imported and used in an app.

這些函式庫擴充套件了 Angular 的基本功能。例如,要嚮應用新增響應式表單,請使用 ng add @angular/forms 新增該函式庫的 npm 套件,再從應用程式碼中,從 @angular/forms 函式庫中匯入 ReactiveFormsModule。同樣,把 Service Worker 函式庫新增到 Angular 應用中是將應用轉換為漸進式 Web 應用程式(PWA)的步驟之一。Angular Material 是一個大型通用函式庫的典範,它提供了一些複雜、可複用,兼具高度適應性的 UI 元件。

Libraries extend Angular's base functionality. For example, to add reactive forms to an app, add the library package using ng add @angular/forms, then import the ReactiveFormsModule from the @angular/forms library in your application code. Similarly, adding the service worker library to an Angular application is one of the steps for turning an application into a Progressive Web App (PWA). Angular Material is an example of a large, general-purpose library that provides sophisticated, reusable, and adaptable UI components.

任何一位應用開發者都可以使用這樣或那樣的函式庫,它們都已經由 Angular 團隊或第三方發佈為 npm 套件。參閱使用已發佈的函式庫

Any app developer can use these and other libraries that have been published as npm packages by the Angular team or by third parties. See Using Published Libraries.

建立函式庫

Creating libraries

如果已經開發出了適合複用的功能,你就可以建立自己的函式庫。這些函式庫可以在你的工作區中本地使用,也可以把它們發佈成 npm 套件,共享給其它專案或其它 Angular 開發者。這些套件可以發佈到 npm 伺服器、一個私有的 npm 企業版伺服器,或一個支援 npm 套件的私有套件管理系統。參閱建立函式庫

If you have developed functionality that is suitable for reuse, you can create your own libraries. These libraries can be used locally in your workspace, or you can publish them as npm packages to share with other projects or other Angular developers. These packages can be published to the npm registry, a private npm Enterprise registry, or a private package management system that supports npm packages. See Creating Libraries.

是否把一些功能打包成函式庫是一種架構決策,類似於決定一個功能應該做成元件還是服務,或決定一個元件的範圍該有多大。

Whether you decide to package functionality as a library is an architectural decision, similar to deciding whether a piece of functionality is a component or a service, or deciding on the scope of a component.

把功能打包成函式庫會強迫函式庫中的工件與應用的業務邏輯分離。這有助於避免各種不良實踐或架構失誤,這些失誤會導致將來很難解耦和複用程式碼。

Packaging functionality as a library forces the artifacts in the library to be decoupled from the application's business logic. This can help to avoid various bad practices or architecture mistakes that can make it difficult to decouple and reuse code in the future.

把程式碼放到一個單獨的函式庫中比簡單地把所有內容都放在一個應用中要複雜得多。它需要更多的時間投入,並且需要管理、維護和更新這個函式庫。不過,當把該函式庫用在多個應用中時,這種複雜性就會得到回報。

Putting code into a separate library is more complex than simply putting everything in one app. It requires more of an investment in time and thought for managing, maintaining, and updating the library. This complexity can pay off, however, when the library is being used in multiple apps.

注意,這裡所說的函式庫是為了供 Angular 應用使用的。 如果想把 Angular 的功能新增到非 Angular 應用中,可以使用 Angular 自訂元素

Note that libraries are intended to be used by Angular apps. To add Angular functionality to non-Angular web apps, you can use Angular custom elements.