更新到 Ionic 4
¥Updating to Ionic 4
从 Ionic 3 更新到 4
¥Updating from Ionic 3 to 4
本指南假设你已经将应用更新到最新版本的 Ionic 3。如果你使用的是 Ionic 1 或 2,请确保遵循 从 Ionic 1 更新到 4 指南。
¥This guide assumes that you have already updated your app to the latest version of Ionic 3. If you are using Ionic 1 or 2, Make sure to follow the Updating from Ionic 1 to 4 Guide instead.
有关从 Ionic 3 到 Ionic 4 的重大更改的完整列表,请参阅 Ionic 框架存储库中的 重大变更文档。
¥For a complete list of breaking changes from Ionic 3 to Ionic 4, please refer to the breaking changes document in the Ionic Framework repository.
我们建议将现有应用从 Ionic 3 迁移到 4 时遵循以下一般流程:
¥We suggest the following general process when migrating an existing application from Ionic 3 to 4:
-
使用
blank
启动器生成一个新项目(参见 启动应用)¥Generate a new project using the
blank
starter (see Starting an App) -
将任何 Angular 服务从
src/providers
复制到src/app/services
¥Copy any Angular services from
src/providers
tosrc/app/services
-
服务应在
@Injectable()
装饰器中包含{ providedIn: 'root' }
。有关详细信息,请参阅 Angular 提供商文档。¥Services should include
{ providedIn: 'root' }
in the@Injectable()
decorator. For details, please see Angular provider docs.
-
-
复制应用的其他根级项目(管道、组件等),请记住目录结构从
src/components
更改为src/app/components
等。¥Copy the app's other root-level items (pipes, components, etc) keeping in mind that the directory structure changes from
src/components
tosrc/app/components
, etc. -
将全局 Sass 样式从
src/app/app.scss
复制到src/global.scss
¥Copy global Sass styling from
src/app/app.scss
tosrc/global.scss
-
逐页或逐个功能地复制应用的其余部分,请记住以下几点:
¥Copy the rest of the application, page by page or feature by feature, keeping the following items in mind:
-
模拟 Shadow DOM 默认开启
¥Emulated Shadow DOM is turned on by default
-
页面/组件 Sass 不应再封装在页面/组件标签中,而应使用 Angular 的
@Component
装饰器的styleUrls
选项¥Page/component Sass should no longer be wrapped in the page/component tag and should use Angular's
styleUrls
option of the@Component
decorator -
RxJS 已从 v5 更新到 v6(参见 RxJS 的变化)
¥RxJS has been updated from v5 to v6 (see RxJS Changes)
-
某些生命周期钩子应该替换为 Angular 的钩子(参见 生命周期事件)
¥Certain lifecycle hooks should be replaced by Angular's hooks (see Lifecycle Events)
-
可能需要更改标记(可用迁移工具,请参阅 标记变更)
¥Markup changes may be required (migration tool available, see Markup Changes)
-
在许多情况下,使用 Ionic CLI 生成新对象然后复制代码也非常有效。例如:ionic g service weather
将创建一个 shell Weather
服务并进行测试。然后可以从旧项目中复制代码,并根据需要进行少量修改。这有助于确保遵循正确的结构。这还会生成用于单元测试的 shell。
¥In many cases, using the Ionic CLI to generate a new object and then copying the code also works very well. For example: ionic g service weather
will create a shell Weather
service and test. The code can then be copied from the older project with minor modifications as needed. This helps to ensure the proper structure is followed. This also generates shells for unit tests.
包名称的变化
¥Changes in Package Name
在 Ionic 4 中,包名称为 @ionic/angular