【问题标题】:How to update Angular 7 to Angular 8 by ng update如何通过 ng update 将 Angular 7 更新到 Angular 8
【发布时间】:2019-05-27 11:39:11
【问题描述】:

我尝试通过在终端中运行 ng update @angular/cli @angular/core 将核心框架和 CLI 的 Angular 项目从版本 7.2.5 更新到 8。

来自终端的响应是“我们分析了您的 package.json,一切似乎都井井有条。干得好!'

我的版本列表:

$ ng version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 7.3.9
Node: 11.14.0
OS: darwin x64
Angular: 7.2.15
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.13.9
@angular-devkit/build-angular      0.13.9
@angular-devkit/build-ng-packagr   0.13.9
@angular-devkit/build-optimizer    0.13.9
@angular-devkit/build-webpack      0.13.9
@angular-devkit/core               7.3.9
@angular-devkit/schematics         7.3.9
@angular/cdk                       7.3.7
@angular/cli                       7.3.9
@angular/material                  7.3.7
@ngtools/json-schema               1.1.0
@ngtools/webpack                   7.3.9
@schematics/angular                7.3.9 (cli-only)
@schematics/update                 0.13.9
ng-packagr                         5.2.0
rxjs                               6.5.2
typescript                         3.2.4
webpack                            4.29.0

【问题讨论】:

标签: angular angular-cli angular-upgrade


【解决方案1】:

截至今天它已经稳定8.0.0 :)

您可能想尝试运行 ng update --all 来更新您的 package.json 文件中的所有软件包。 但是,如果它发现其他包之间没有满足某些相互依赖关系,则可能不会继续进行。 在这种情况下,您可能还需要添加 --force 选项。

或者,您可以选择更量身定制的方法并手动选择要更新的软件包(正如 Veeraragavan 的回答所建议的那样),但您不再需要添加 @next 版本,因为所有 Angular 软件包 8.0.0 版本都是稳定的截至目前。

【讨论】:

    【解决方案2】:

    截至今天,Angular 8 还不是一个稳定的版本。

    但如果你真的想更新非稳定版本,请在包名称末尾使用 @next

    ng 更新@angular/cli@next @angular/core@next

    【讨论】:

    • 我在 2020 年 1 月 16 日遇到了这个问题。我相信 Angular 8 现在被认为是一个稳定的版本。
    【解决方案3】:

    Angular 团队让升级变得比以前容易得多。您可以使用 Angular 更新指南获取将项目从 Angular 版本更新到另一个版本所需运行的命令。

    前往该交互式指南并将 7.1.4 指定为当前版本,并将目标版本指定为 v8.3.2,然后单击 Show me how to update 按钮

    指定版本后,您会收到一条警告说我们不建议跨多个主要版本移动。由于我们正在从 v7 迁移到 v8:

    Angular 8 使用 TypeScript 3.4,read more about errors that might arise from improved type checking

    确保您使用的是Node 10 or later

    在你的命令行界面中运行以下命令:

    $ ng update
    
    

    这将分析您项目的package.json 文件,并为您提供使用所需命令更新的软件包列表:

        We analyzed your package.json, there are some packages to update:
    
          Name                               Version                  Command to update
         --------------------------------------------------------------------------------
          @angular/cli                       7.1.4 -> 8.3.19          ng update @angular/cli
          @angular/core                      7.1.4 -> 8.2.14          ng update @angular/core
          rxjs                               6.3.3 -> 6.5.3           ng update rxjs
    
    
        There might be additional packages that are outdated.
        Run "ng update --all" to try to update all at the same time.
    
    

    那么,让我们首先使用以下命令将核心框架和 CLI 分别更新到 v8.2.14v8.3.19

    $ ng update @angular/cli @angular/core
    
    

    这会将核心框架和 CLI 更新到 Angular 8:

        Updating package.json with dependency @angular/compiler @ "8.2.14" (was "7.1.4")...
        Updating package.json with dependency @angular/language-service @ "8.2.14" (was "7.1.4")...
        Updating package.json with dependency @angular/forms @ "8.2.14" (was "7.1.4")...
        Updating package.json with dependency @angular/platform-browser @ "8.2.14" (was "7.1.4")...
        Updating package.json with dependency @angular/platform-browser-dynamic @ "8.2.14" (was "7.1.4")...
        Updating package.json with dependency @angular/cli @ "8.3.19" (was "7.1.4")...
        Updating package.json with dependency @angular/animations @ "8.2.14" (was "7.1.4")...
        Updating package.json with dependency @angular/common @ "8.2.14" (was "7.1.4")...
        Updating package.json with dependency zone.js @ "0.9.1" (was "0.8.26")...
        Updating package.json with dependency rxjs @ "6.5.3" (was "6.3.3")...
        Updating package.json with dependency @angular/router @ "8.2.14" (was "7.1.4")...
        Updating package.json with dependency @angular/core @ "8.2.14" (was "7.1.4")...
        Updating package.json with dependency @angular/compiler-cli @ "8.2.14" (was "7.1.4")...
        Updating package.json with dependency typescript @ "3.5.3" (was "3.1.6")...
    
    

    注意:在 Windows 上,我必须在 ng update @angular/cli @angular/core 之后运行 npm install 才能安装新版本的依赖项。

    现在,让我们使用以下命令检查新版本的 Angular:

    $ ng --version
    
    

    这是命令的输出:

    Angular CLI: 8.3.19
    Node: 10.16.3
    OS: win32 ia32
    Angular: 8.2.14
    ... animations, common, compiler, compiler-cli, core, forms
    ... language-service, platform-browser, platform-browser-dynamic
    ... router
    
    Package                           Version
    -----------------------------------------------------------
    @angular-devkit/architect         0.11.4
    @angular-devkit/build-angular     0.11.4
    @angular-devkit/build-optimizer   0.11.4
    @angular-devkit/build-webpack     0.11.4
    @angular-devkit/core              7.1.4
    @angular-devkit/schematics        8.3.19
    @angular/cli                      8.3.19
    @ngtools/webpack                  7.1.4
    @schematics/angular               8.3.19
    @schematics/update                0.803.19
    rxjs                              6.5.3
    typescript                        3.5.3
    webpack                           4.23.1
    
    

    您可以看到我们已经成功将 Angular CLI 更新到 v8.3.19,Angular 更新到 v8.2.14 以及不同的核心包。甚至 TypeScript 也被撞到了 v3.5.3

    我从这个update guide 和这个official post 得到这些指示

    【讨论】:

      【解决方案4】:

      除了关于 ng update 命令的其他答案,请注意 Angular 版本 7 和 8 之间的重大更改需要注意。

      特别是:

      • 如果您使用旧版 HttpModuleHttp 服务,请切换到 HttpClientModuleHttpClient 服务。 HttpClient 简化了 默认人体工程学(您不再需要映射到 JSON)现在 支持类型化的返回值和拦截器。阅读更多 angular.io
      • 确保您使用的是Node 10 or later

      Angular 在他们的Angular Update Guide 中详细描述了这个过程。

      【讨论】:

        【解决方案5】:

        如果你重复

        ng 更新 @angular/cli @angular/core

        它将升级到下一步,即 8

        【讨论】:

          【解决方案6】:

          通过运行更新到版本 8 的核心框架和 CLI

          ng update @angular/cli@8 @angular/core@8 在您的终端中

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-10-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-02-14
            • 1970-01-01
            • 2015-04-18
            • 1970-01-01
            相关资源
            最近更新 更多