填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

弃用的 API 和特性

Deprecated APIs and Features

Angular 力图兼顾创新与稳定。但有时,API 和特性已经过时,需要进行删除或替换,以便 Angular 可以及时跟上新的最佳实践、依赖项变更或者 Web 平台自身的变化。

Angular strives to balance innovation and stability. Sometimes, APIs and features become obsolete and need to be removed or replaced so that Angular can stay current with new best practices, changing dependencies, or changes in the (web) platform itself.

为了让这些转换变得尽可能简单,我们会在删除 API 和特性之前先弃用它们一段时间。让你有时间把应用更新到最新的 API 和最佳实践。

To make these transitions as easy as possible, we deprecate APIs and features for a period of time before removing them. This gives you time to update your apps to the latest APIs and best practices.

本指南包含了当前不推荐使用的所有 Angular API 和特性的汇总表。

This guide contains a summary of all Angular APIs and features that are currently deprecated.

v6 或更早版本中已弃用的特性和 API 将会在版本 9 或更高级版本中删除。要了解 Angular 中关于弃用和删除的实践,参见Angular 发布实践

Features and APIs that were deprecated in v6 or earlier are candidates for removal in version 9 or any later major version. For information about Angular's deprecation and removal practices, see Angular Release Practices.

有关如何更新到最新 Angular 版本的分步说明,请参阅 update.angular.io 上的交互式更新指南。

For step-by-step instructions on how to update to the latest Angular release, use the interactive update guide at update.angular.io.

已弃用的 API

Deprecated APIs

本节包含所有当前已弃用的 API 的完整列表,其中包含一些可帮助你规划如何迁移到其替代品的详细信息。

This section contains a complete list all of the currently-deprecated APIs, with details to help you plan your migration to a replacement.

提示:在本文档站的 API参考手册部分,不推荐使用的 API 会用删除线标记出来。你可以按状态: 已弃用来过滤 API 列表。

Tip: In the API reference section of this doc site, deprecated APIs are indicated by strikethrough. You can filter the API list by Status: deprecated.

@angular/common

API

替代品

Replacement

已宣布弃用

Deprecation announced

备注

Notes

DeprecatedI18NPipesModuleCommonModulev5

参见管道

See Pipes

DeprecatedCurrencyPipeCurrencyPipev5

参见管道

See Pipes

DeprecatedDatePipeDatePipev5

参见管道

See Pipes

DeprecatedDecimalPipeDecimalPipev5

参见管道

See Pipes

DeprecatedPercentPipePercentPipev5

参见管道

See Pipes

@angular/core

API

替代品

Replacement

已宣布弃用

Deprecation announced

备注

Notes

CollectionChangeRecordIterableChangeRecordv4

none

DefaultIterableDiffer

不适用

n/a

v4

不属于公共 API。

Not part of public API.

defineInjectableɵɵdefineInjectablev8

仅在生成的代码中使用。没有源码会依赖这个 API。

Used only in generated code. No source code should depend on this API.

injectɵɵinjectv8

仅在生成的代码中使用。没有源码会依赖这个 API。

Used only in generated code. No source code should depend on this API.

ReflectiveInjectorInjector.createv5

参见 ReflectiveInjector

See ReflectiveInjector

ReflectiveKey

none

v5

none

RenderComponentType

RendererType2Renderer2

RendererType2and Renderer2

v4

none

RendererRenderer2v4

none

RootRendererRendererFactory2v4

none

ViewEncapsulation.NativeViewEncapsulation.ShadowDomv6

使用渲染器的原生封装方式(Native)。参见 view.ts

Use the native encapsulation mechanism of the renderer. See view.ts.

@angular/core/testing

API

替代品

Replacement

已宣布弃用

Deprecation announced

备注

Notes

TestBed.deprecatedOverrideProvider()TestBed.overrideProvider()v7

none

TestBedStatic.deprecatedOverrideProvider()TestBedStatic.overrideProvider()v5

none

@angular/forms

API

替代品

Replacement

已宣布弃用

Deprecation announced

备注

Notes

NgFormSelectorWarning

不适用

n/a

v6

参见 ngForm

See ngForm.

@angular/router

API

替代品

Replacement

已宣布弃用

Deprecation announced

备注

Notes

preserveQueryParamsqueryParamsHandlingv4

none

@angular/upgrade

API

替代品

Replacement

已宣布弃用

Deprecation announced

备注

Notes

所有入口点

All entry points

@angular/upgrade/staticv5

参见从 AngularJS 升级

See Upgrading from AngularJS.

@angular/upgrade/static

API

替代品

Replacement

已宣布弃用

Deprecation announced

备注

Notes

getAngularLibgetAngularJSGlobalv5

参见从 AngularJS 升级

See Upgrading from AngularJS.

setAngularLibsetAngularJSGlobalv5

参见从 AngularJS 升级

See Upgrading from AngularJS.

已弃用的特性

Deprecated features

本节列出了所有当前已弃用的特性,包括模板语法、配置选项,以及前面已弃用的 API 部分未列出的其它弃用。它还包括已弃用的 API 用例或 API 组合,以增强上述信息。

This section lists all of the currently-deprecated features, which includes template syntax, configuration options, and any other deprecations not listed in the Deprecated APIs section above. It also includes deprecated API usage scenarios or API combinations, to augment the information above.

组件样式

Component styles

刺穿 Shadow DOM 的 CSS 组合符已经弃用,并且主要的浏览器和工具都已删除它。因此,在 v4 中,Angular 也弃用了对 /deep/>>>::ng-deep 的支持。在彻底删除它之前,我们首选::ng-deep ,以便和各种工具实现更广泛的兼容。

The shadow-piercing descendant combinator is deprecated and support is being removed from major browsers and tools. As such, in v4 we deprecated support in Angular for all 3 of /deep/, >>> and ::ng-deep. Until removal, ::ng-deep is preferred for broader compatibility with the tools.

欲知详情,参阅“组件样式”一章中的 /deep/,>>> 和 :: ng-deep

For more information, see /deep/, >>>, and ::ng-deep in the Component Styles guide.

<template> 标签

<template> tag

<template> 标记在 v4 中已弃用,以避免与同名的 DOM 元素冲突(例如当使用 Web Components 时)。请改用 <ng-template>。欲知详情,请参阅预先编译一章。

The <template> tag was deprecated in v4 to avoid colliding with the DOM's element of the same name (such as when using web components). Use <ng-template> instead. For more information, see the Ahead-of-Time Compilation guide.

ngForm 元素选择器

ngForm element selector

在 v6 中不再支持使用 ngForm 元素选择器了。它已被弃用,以便与其它核心 Angular 选择器保持一致,那些选择器通常写作中线形式(kebab-case)。

Support for using ngForm element selector was deprecated in v6. It has been deprecated to be consistent with other core Angular selectors, which are typically written in kebab-case.

已弃用:

Deprecated:

<ngForm #myForm="ngForm">
      
      <ngForm #myForm="ngForm">
    

替代品:

Replacement:

<ng-form #myForm="ngForm">
      
      <ng-form #myForm="ngForm">
    

当使用这个已弃用的 ngForm 选择器时, NgFormSelectorWarning指令只是用来显示警告信息的。

The NgFormSelectorWarningdirective is solely used to display warnings when the deprecated ngForm selector is used.

和响应式表单一起使用 ngModel

ngModel with reactive forms

版本 6 中已弃用:和响应式表单指令一起使用 ngModelngModelChange

Support for using the ngModel input property and ngModelChange event with reactive form directives was deprecated in version 6.

欲知详情,请参阅 FormControlDirectiveFormControlName的用法说明部分。

For more information, see the usage notes for FormControlDirectiveand FormControlName.

Service Worker 的 versionedFiles

Service worker versionedFiles

在 Service Worker 的配置文件 ngsw-config.json 中,versionedFilesfiles 的行为是完全一样的。从 v6 开始,弃用 versionedFiles,请改用files

In the service worker configuration file ngsw-config.json, versionedFiles and files have the same behavior. As of v6, versionedFiles is deprecated; use files instead.

欲知详情,请参阅Service Worker 配置

For more information, see Service Worker Configuration.

ReflectiveInjector

在 v5 中,Angular 用 StaticInjector 代替了 ReflectiveInjector。该注入器不再需要 Reflect 的腻子脚本,对大部分开发人员来说都显著减小了应用的体积。

In v5, Angular replaced the ReflectiveInjector with the StaticInjector. The injector no longer requires the Reflect polyfill, reducing application size for most developers.

之前:

Before:

ReflectiveInjector.resolveAndCreate(providers);
      
      ReflectiveInjector.resolveAndCreate(providers);
    

之后:

After:

Injector.create({providers});
      
      Injector.create({providers});
    

使用 Intl API 的管道

Pipes using Intl API

Angular 曾依赖浏览器的 i18n API,来提供数字、日期和货币的格式化功能。这种做法意味着大多数应用都需要使用腻子脚本,用户还会看到不同浏览器上的结果不一致,并且常见的格式(例如货币管道)不能满足开发者的期望。

Angular used to rely on the browser to provide number, date, and currency formatting using browser i18n APIs. This practice meant that most apps needed to use a polyfill, users were seeing inconsistent results across browsers, and common formats (such as the currency pipe) didn’t match developer expectations out of the box.

在 4.3 版本中,Angular 引入了新的数字,日期和货币管道,可以提高浏览器的标准化水平,并且无需 i18n 的腻子脚本。这些管道使用通用语言环境数据仓库(CLDR)而不是 JS Intl API,以便提供更广泛的本地化支持。

In version 4.3, Angular introduced new number, date, and currency pipes that increase standardization across browsers and eliminate the need for i18n polyfills. These pipes use the Unicode Common Locale Data Repository (CLDR) instead of the JS Intl API to provide extensive locale support.

在 5.0.0 版本中,Angular 修改了它的标准管道,以便使用 CLRD 来实现。那时,Angular 还添加了DeprecatedI18NPipesModule和相关的 API 来提供对原有行为的限时访问。如果你要使用这些 Deprecated* 管道,可参见 Angular 的变更日志日期格式的映射表

In version 5.0.0, Angular updated its standard pipes to use the CLRD implementation. At that time, Angular also added DeprecatedI18NPipesModuleand related APIs to provide limited-time access to the old behavior. If you need to use these Deprecated* pipes, see Angular change log and the Date Formats mappings.

提醒:如果你用到了这些 Deprecated* 管道,那么你应该尽快迁移到上面列出的新 API。这些已弃用的 API 都是下一个主版本中准备删除的 API。

Reminder: If you use these Deprecated* pipes, you should migrate to the current APIs listed above as soon as possible. These deprecated APIs are candidates for removal in the next major release.

loadChildren 字符串语法

loadChildren string syntax

当 Angular 第一次引入惰性路由时,还没有浏览器能支持动态加载额外的 JavaScript。因此 Angular 创建了自己的方案,所用的语法是 loadChildren: './lazy/lazy.module#LazyModule' 并且还构建了一些工具来支持它。现在,很多浏览器都已支持 ECMAScript 的动态导入,Angular 也正朝着这个新语法前进。

When Angular first introduced lazy routes, there wasn't browser support for dynamically loading additional JavaScript. Angular created our own scheme using the syntax loadChildren: './lazy/lazy.module#LazyModule' and built tooling to support it. Now that ECMAScript dynamic import is supported in many browsers, Angular is moving toward this new syntax.

在 v8 中,不推荐使用 loadChildren路由规范的字符串语法,loadChildren支持使用基于 import() 的新语法。

In v8, the string syntax for the loadChildrenroute specification was deprecated, in favor of new syntax that uses import() syntax.

之前:

Before:

const routes: Routes = [{ path: 'lazy', // The following string syntax for loadChildren is deprecated loadChildren: './lazy/lazy.module#LazyModule' }];
      
      const routes: Routes = [{
  path: 'lazy',
  // The following string syntax for loadChildren is deprecated
  loadChildren: './lazy/lazy.module#LazyModule'
}];
    

之后:

After:

const routes: Routes = [{ path: 'lazy', // The new import() syntax loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule) }];
      
      const routes: Routes = [{
  path: 'lazy',
  // The new import() syntax 
  loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)
}];
    

v8 更新:当你升级到版本 8 时,ng update命令会自动执行转换。在版本 7 之前,import() 语法只能在 JIT 模式下运行(和视图引擎一起)。

v8 update: When you update to version 8, the ng updatecommand performs the transformation automatically. Prior to version 7, the import() syntax only works in JIT mode (with view engine).

ActivatedRoute 的 params 和 queryParams 属性

ActivatedRoute params and queryParams properties

ActivatedRoute 包含两个属性 ,它们的能力低于它们的替代品,在将来的 Angular 版本中可能会弃用。

ActivatedRoute contains two properties that are less capable than their replacements and may be deprecated in a future Angular version.

属性

Property

替代品

Replacement

paramsparamMap
queryParamsqueryParamMap

欲知详情,参见路由器指南

For more information see the Router guide.

已删除的 API

Removed APIs

从 8.0.0 开始,已删除下列 API:

The following APIs have been removed starting with version 8.0.0:

Package

API

替代品

Replacement

备注

Notes

@angular/http

全部导出

All exports

@angular/common/http

参见下文

See below.

@angular/http/testing

全部导出

All exports

@angular/common/http/testing

参见下文

See below.

@angular/platform-browserDOCUMENT

DOCUMENT in @angular/common

@angular/common DOCUMENT

使用 ng update更新到版本 8 时会自动更改。

Updating to version 8 with ng updatechanges this automatically.

@angular/http

已删除了整个 @angular/http包。请改用 @angular/common/http

The entire @angular/httppackage has been removed. Use @angular/common/httpinstead.

新的 API 用一种更小、更简单、更强大的方式来在 Angular 中发起 HTTP 请求。新的 API 简化成了更人性化的默认设计:不用再通过调用 .json() 方法进行映射。它还支持带类型的返回值,以及拦截器。

The new API is a smaller, easier, and more powerful way to make HTTP requests in Angular. The new API simplifies the default ergonomics: There is no need to map by invoking the .json() method. It also supports typed return values and interceptors.

要更新你的应用:

To update your apps:

有关使用 @angular/common/http 的更多信息,请参见 HttpClient 指南

For more information about using @angular/common/http, see the HttpClient guide.

@angular/http

@angular/common/http 中最接近的替代品

Closest replacement in @angular/common/http

BaseRequestOptionsHttpRequest
BaseResponseOptionsHttpResponse
BrowserXhr
ConnectionHttpBackend
ConnectionBackendHttpBackend
CookieXSRFStrategyHttpClientXsrfModule
HeadersHttpHeaders
HttpHttpClient
HttpModuleHttpClientModule
JsonpHttpClient
JSONPBackendJsonpClientBackend
JSONPConnectionJsonpClientBackend
JsonpModuleHttpClientJsonpModule
QueryEncoderHttpUrlEncodingCodec
ReadyStateHttpBackend
RequestHttpRequest
RequestMethodHttpClient
RequestOptionsHttpRequest
RequestOptionsArgsHttpRequest
ResponseHttpResponse
ResponseContentTypeHttpClient
ResponseOptionsHttpResponse
ResponseOptionsArgsHttpResponse
ResponseTypeHttpClient
URLSearchParamsHttpParams
XHRBackendHttpXhrBackend
XHRConnectionHttpXhrBackend
XSRFStrategyHttpClientXsrfModule
@angular/http/testing

@angular/common/http/testing 中最接近的替代品

Closest replacement in @angular/common/http/testing

MockBackendHttpTestingController
MockConnectionHttpTestingController