Angular8升级至Angular13遇到的问题

2023-05-16

根据项目需求,需要把Angular版本从8升级到13,无法从8直接升至13,需要一级一级的升级,本文介绍了在升级Angular版本的时候的一种报错和解决办法。

一、开始之前

首先确保你 Node.js >= 12.20

创建新的分支,或者使用其他方式备份当前项目

删除项目下 yarn.lock 或 package-lock.jso

二、升级步骤(一级一级的升级)

     首先要阅读下官方升级文档:Angular Update Guide

  1. 升级相关依赖前往 update.angular.io 将项目升级到 Angular (9-13版本)。
  2. 如果你有单独使用 @angular/cdk 请执行 ng update @angular/cdk@(9-13版本)
  3. 升级 NG-ZORRO,运行 ng update ng-zorro-antd@(9-13版本)
  4. 升级 NG-ALAIN,运行 ng update ng-alain(9-13版本)

三、常见问题

错误1:WARNING in xxx is part of the TypeScript compilation but it’s unused.Add only entry points to the ‘files’ or ‘include’ properties in your tsconfig.

// 在exclude后加上以下信息
 "files": ["../src/main.ts", "../src/polyfills.ts"],
 "include": ["src/**/*.d.ts"]

错误2:Repository is not clean. Please commit or stash any changes before updating.

ng update --all  --force --allow-dirty

错误3: Package ‘@angular/core’ is not a dependency…

类似以上错误重新安装依赖

npm i

错误4:ERROR in ./src/styles.less (./node_modules/css-loader/dist/cjs.js??ref–14-1!./node_modules/postcss-loader/src??embedded!./node_modules/less-loader/dist/cjs.js??ref–14-3!./src/styles.less) Module build failed (from ./node_modules/less-loader/dist/cjs.js):@import ‘~@delon/theme/styles/index’;Can’t resolve ‘@delon/theme/styles/index.less’ in ‘xxx;in xxx\src\styles.less (line 2, column 0);@import ‘~@delon/theme/styles/default’;Can’t resolve ‘@delon/theme/styles/default.less’ in ‘xxx\src\app\layout\passport’

路径有变化, 把 @import ‘~@delon/theme/styles/index’; 多余的一层目录去掉:styles 报错的目录文件均需要去除多余目录

如果路径对着,但是还报如下错误:

angular.json未配置样式路径导致:

"stylePreprocessorOptions": {
  "includePaths": [
    "node_modules/"
  ]
}

错误5 src/app/layout/default/header/components/taskmange.component.ts:33:28 – error NG1001: @ViewChild options must be an object literal @ViewChild(“taskDrawer”, null) taskDrawer;

原因:ViewChild需要两个参数,并没有提供opts

官网解释:

static – whether or not to resolve query results before change detection runs (i.e. return static results only). If this option is not provided, the compiler will fall back to its default behavior, which is to use query results to determine the timing of query resolution. If any query results are inside a nested view (e.g. *ngIf), the query will be resolved after change detection runs. Otherwise, it will be resolved before change detection runs.

此段解释在中文文档中并没有被翻译,大体意思如下:

static – 是否在更改检测运行之前解析查询结果(即只返回静态结果)。如果不提供此选项,编译器将退回到其默认行为,即使用查询结果来确定查询解析的时间。如果任何查询结果位于嵌套视图中(例如*ngIf),则在运行更改检测后将解析该查询。否则,它将在变更检测运行之前被解析。

@ViewChild("taskDrawer", {static: true}) taskDrawer;

 // 或者
 @ViewChild("taskDrawer", {static: false}) taskDrawer;

// 或者 
 @ViewChild("taskDrawer") taskDrawer;

根据官方提供的,不同场景设置。

错误6:样式不见了

// angular.json 文件引入
"styles": [
	 "src/styles.less",
     "./node_modules/ng-zorro-antd/ng-zorro-antd.min.css"
],
// styles.less文件引入
@import '~ng-zorro-antd/ng-zorro-antd.less';
@import '~ng-zorro-antd/style/entry.less'; /* 引入基本样式 */
@import '~ng-zorro-antd/button/style/entry.less'; /* 引入组件样式 */

错误7:ERROR in src/app/core/i18n/i18n.service.ts:13:24 – error TS2307: Cannot find module ‘date-fns/locale/en’.13 import * as df_en from ‘date-fns/locale/en’;

import { enUS as dfEn, zhCN as dfZhCn, zhTW as dfZhTw, vi as dfVi } from 'date-fns/locale';

错误8: import { STColumn, STComponent, STReq, STRequestOptions, STRes } from ‘@delon/abc/table’;

// https://github.com/ng-alain/ng-alain/issues/1569 里有说明路径变化,更改即可
import { STColumn, STComponent, STReq, STRequestOptions, STRes } from '@delon/abc/st';

错误9:error TS2307: Cannot find module ‘date-fns/distance_in_wor,import * as distanceInWordsToNow from ‘date-fns/distance_in_words_to_now’;

import formatDistanceToNow from 'date-fns/formatDistanceToNow';

错误10:polyfills.js:7568 Unhandled Promise rejection: R3InjectorError(AppModule)[ApplicationModule -> ApplicationRef ->ApplicationInitStatus -> InjectionToken Application Initializer -> [object Object] -> StartupService -> ACLService -> ACLService ->ACLService]:NullInjectorError: No provider for ACLService! ; Zone: ; Task: Promise.then ; Value: NullInjectorError: R3InjectorError(AppModule)[ApplicationModule -> ApplicationRef -> ApplicationInitStatus -> InjectionToken Application Initializer -> [object Object] ->StartupService -> ACLService -> ACLService -> ACLService]:NullInjectorError: No provider for ACLService!

// 仔细看错误发现No provider for ACLService,则在app.module.ts中引入 ACLService ,缺什么引什么
  providers: [
    // 略
    ACLService,
    AlainConfigService,
  ],

错误11:Package ‘@angular/core’ is not a dependency…

npm i

错误12:ERROR in Failed to list lazy routes: Unknown module ‘E:/xxx/src/app/app.module#AppModule’.

错误12ERROR in Failed to list lazy routes: Unknown moduleE:/xxx/src/app/app.module#AppModule’.

错误13:Angular11 升级报错:Angular Forms error: Two incompatible decorators on class

在Google搜索了解决办法,发现遇到这种情况的人不少,但是都没有明确的解决办法,或者解决办法在本项目不适用。随后查阅了Angular的文档,发现通过以下方法可以解决问题。
在tsconfig.json中添加以下代码:

{
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  },
}

错误14:typescript不兼容问题 @angular/compiler-cli@8.0.3 requires a peer of typescript@>=3.4 ❤️.5 but none is installed. You must install peer dependencies yourself.

解决办法:
安装兼容版本
运行命令

npm i typescript@3.4.3

错误15:import { NzMessageService, UploadFile } from ‘ng-zorro-antd’;

组件导入路径发生了变化

import { NzMessageService } from 'ng-zorro-antd/message';
import { NzUploadFile } from 'ng-zorro-antd/upload';

错误16:import { NzModalService } from ‘ng-zorro-antd’;

组件导入路径发生了变化

import { NzModalService } from 'ng-zorro-antd/modal';

错误17:自定义主题色不起作用

angular.json 删除引入的组件主题色

src/styles.less 里引入预定义主题文件

@import "../node_modules/ng-zorro-antd/ng-zorro-antd.less";

@import './styles/theme';
@import './styles/index';

错误18:Can’t bind to ‘formGroup’ since it isn’t a known property of ‘form’

使用 Reactive Forms 需要额外引入 ReactiveFormsModule,可以参考官方文档(https://angular.io/guide/reactive-forms)。

错误19:Angular12报错(resize-observer-polyfill) TS2717:Property contentRect must be of type DOMRectReadOnly

解决办法:
(1) 由于报错的是第三方类库,只能等待第三方类库去修复该错误。

(2) 在第三方修复之前,可以禁用第三方类库的检查。

在项目根目录的 TS 配置文件 tsconfig.json 中新增 skipLibCheck 属性。

{
  "compilerOptions": {
 	。。。。。。

    "skipLibCheck": true   <---- 增加该配置
  }
}

错误20:

解决方案:npm install --save-dev raw-loader

npm install --save-dev raw-loader

错误21:多次注入

platformBrowserDynamic被多次注入(一般为main.ts和app.module.ts),删除多余的,保留一个,可以删App.module.t

错误22:angular 从11.x更新到12.x 收到DON‘T USE IT FOR PRODUCTION!警告

angular从11.x版本升级到12.x版本后,会收到

This is a simple server for use in testing or debugging Angular applications locally.
It hasn’t been reviewed for security issues.

DON’T USE IT FOR PRODUCTION!

的警告,除了升级其他的并未修改,11.x版本的运行ng serve并没有这个警告。

我是升级到12.2.17版本的,重新运行:

ng update @angular/cli --migrate-only --from=11.2.0 --to=12.2.17

运行后在ng serve那个警告就消失了

错误23:Git无法提交(Must use import to load ES Module: /Users/cipchk/Desktop/work/ng-alain/node_modules/@angular/compiler/fesm2015/compiler.mjs)

升级步骤是逐步运行,每一步都需要 git commit,注释掉 .husky/pre-commit 中的 npx 开头的行,在升级完成后再次打开。

错误24:An unhandled exception occurred: Directory import ‘/media/fuchaoyang/f5558d47-6c02-44ae-89da-2817f50425cf/angular12/licadmin/node_modules/@angular-devkit/build-angular/src/dev-server’ is not supported resolving ES modules

版本不兼容所致

// 升级前
"@angular-devkit/build-angular": "~12.2.18" 
// 升级后
"@angular-devkit/build-angular": "~13.3.9",

错误25:export ‘startWith’ (imported as ‘startWith’) was not found in ‘rxjs’

rxjs版本过低所致,升级版本

// 升级前
 "rxjs": "~6.5.3"

// 升级后
"rxjs": "~7.5.0"

错误26:Error: src/app/routes/dtreportmodule/data-report/pandect/pandect.component.ts:6:10 – error TS2305: Module ‘”@delon/chart”‘ has no exported member ‘G2TimelineData’.

组件路径发生了变化

import { G2TimelineData } from '@delon/chart/timeline';

错误27:编译后git出现了很多缓存编译文件

更新目录.gitignore文件增加如下忽略项:

# See http://help.github.com/ignore-files/ for more about ignoring files.

# Compiled output
/dist
/tmp
/out-tsc
/bazel-out

# Node
/node_modules
npm-debug.log
yarn-error.log

# IDEs and editors
.idea/
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# Visual Studio Code
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*

# Miscellaneous
/.angular/cache
.sass-cache/
/connect.lock
/coverage
/libpeerconnection.log
testem.log
/typings

# System files
.DS_Store
Thumbs.db

错误28:抽屉组件内部自定义内容无法展示

自定义ng-content 外面需要 包起来

错误29:antd-Table组件渲染数据时出现第一行空白行

antd table 加上 [nzScroll]=”{ x: ‘1500px’ }” 出现空白行

解决办法:

:host ::ng-deep .ant-table-measure-now{
  // display: none;
  visibility: collapse;
}

30.报错如下:

解决方式: 在tsconfig.json里新增”skipLibCheck”: true

欢迎在评论区交流。如果文章对你有所帮助,不要忘了点上宝贵的一赞!

我的博客原文:我的博客

我的编程导航:程序员摸鱼导航

前端热门面试题:Angular Update Guide

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular8升级至Angular13遇到的问题 的相关文章

  • 阅读笔记:TF - IDF 原理

    今天查阅 TF IDF 资料 xff0c 发现百度百科里面提供了一个例子 xff0c 解释的很清楚 xff0c 记下来备用 原文链接 xff1a https baike baidu com item tf idf 8816134 fr 61
  • 词向量语义匹配:欧氏距离和余弦相似度,选择哪一个?

    最近做自然语言处理算法 xff0c 需要根据词向量判断两个词汇的相似度 面临两个选择 xff1a 欧氏距离和余弦相似度 选择哪一个好呢 xff1f 一 概念图解 为便于理解这个问题 xff0c 假设词向量是二维的 我们分析一下这两种方法计算
  • 一分钟理解 AP(Affinity Propagation) 亲和⼒传播算法

    这篇博客发出来后 xff0c 我用 Rust 复现代码出现问题 为此 xff0c 我对对照了 sklearn 的相关代码 xff0c 反复比较了两天 xff0c 发现一处 bug xff0c 把 43 61 误写成了 61 xff0c 导致
  • 机器学习:准确率(Precision)、召回率(Recall)、F值(F-Measure)、ROC曲线、PR曲线

    增注 xff1a 虽然当时看这篇文章的时候感觉很不错 xff0c 但是还是写在前面 xff0c 想要了解关于机器学习度量的几个尺度 xff0c 建议大家直接看周志华老师的西瓜书的第2章 xff1a 模型评估与选择 xff0c 写的是真的很好
  • (5)细菌实验分组

    描述 有一种细菌分为A B两个亚种 xff0c 它们的外在特征几乎完全相同 xff0c 仅仅在繁殖能力上有显著差别 xff0c A亚种繁殖能力非常强 xff0c B亚种的繁殖能力很弱 在一次为时一个小时的细菌繁殖实验中 xff0c 实验员由
  • Python you-get 库 + FFmpeg 工具下载B站视频

    Python you get 库 43 FFmpeg 工具下载B站视频 电脑系统 xff1a Windows 10 准备阶段 xff08 安装you get和FFmpeg xff09 安装其实非常简单 xff0c 出现问题可以在网上参考其他
  • 虚拟机的创建、Linux相关基本命令等

    作业内容 xff1a 1 重新创建一个虚拟机 xff0c 熟悉下步骤 创建一个新的虚拟机 xff1a 1 点击 xff1a 创建新的虚拟机 2 下一步 3 提前下载一个镜像文件 xff0c 选择该文件即可 xff08 下载地址 xff1a
  • 解决mysql8.0主从配置,从库连接报错:Authentication plugin ‘caching_sha2_password‘ reported error

    其他配置都是按照网上分享的流程 xff0c 但是show slave status 查看从库状态时 xff0c Slave IO Running 61 connecting xff0c 这个状态是不对的 xff08 正常的Slave IO
  • windows10下visual studio 2019安装以及cuda11配置

    安装visual studio 2019 进入官方的下载页面 xff0c 可能需要登录 xff0c 登录后选取社区版下载 注意 xff0c 这里只是下载安装器 xff0c 真正的安装会在后续执行文件 xff0c 配置安装目录后 xff0c
  • Go语言基础语法_1_2021-10-28

    序言 xff1a 本身 xff0c 我是一个Java开发者 xff0c 但是最近在学习Java虚拟机 xff0c 但是看来看去总是不明白Java虚拟机是怎样实现的 xff0c 而有一本书是 自己动力手写Java虚拟机 xff0c 是用go语
  • debian(Linux)系统下安装jdk1.8

    第一步 xff1a 下载安装包 下载Linux环境下的jdk8 xff0c 请去 xff08 Java Downloads Oracle xff09 中下载jdk的安装文件 xff1b 由于我的Linux是64位的 xff0c 因此我下载j
  • Codeblocks自动代码格式化

    在代码框里点右键 xff0c 按Format use Astyle就会自动代码格式化了 但是它默认的风格是大括号另起一行 xff0c 很不习惯 xff0c 实际上是可以改的 1 Setting gt Editor gt Source For
  • 无法找到输出设备?

    希望我的方法能帮到你 我的电脑是联想 系统是win11 上次更新之后 扬声器就不能用了 右下角的喇叭是一个叉叉 一直没有放在心上 今天解决了一下 右键显示叉叉的喇叭 gt 希望我的方法 能帮到你
  • java程序设计-第一章

    第一章 教材知识点 概念1 2 什么是计算机1 3 编程语言1 4操作系统 Operating System1 5 操作系统1 6 1 7 1 8 1 10关键术语 教材quiz1 如何换行要点12 打印表格 要点2 域宽3 求半径5 5的
  • 将PHP项目部署到服务器

    CONTENT 流程1 租服务器2 配置环境3 测试4 WINScp上传文件 项目语言php xff0c 连接mysql 流程 租服务器 gt 配置环境 gt 测试php和数据库环境 gt 上传文件 gt 运行 1 租服务器 步骤 xff1
  • pandas scatter_matrix使用

    示例来自 Phython机器学习基础教程 Introduction to Machine Learning with Python 德 Andreas C M ller 美 Sarah Guido 著 张亮 xff08 hysic xff0
  • tensorflow.compat.v1

    tensorflow高版本适配低版本解决方法 IDE PyCharm tf版本 2 4 问题 卸载tensorflow1 14重装了最新的2 4之后 xff0c 使用如下方法 xff0c IDE提示找不到compat模块 span clas
  • 2014.软院.Problem B. 最长连续等差子数列

    题目描述 给定一个长度为N的整数数列 xff0c 你需要在其中找到最长的连续子数列的长度 xff0c 并满足这个子序列是等差的 注意公差小于或等于0的情况也是允许的 输入格式 第一行为数据组数T xff08 1 lt 61 T lt 61
  • Android studio安卓虚拟机无法启动

    1 问题 曾经配置过并成功启动过安卓虚拟机 xff0c 时隔若干年后无法启动 xff0c 并总是自动杀死 xff0c 除此之外没有任何有用信息 xff1a The emulator process for AVD was killed 2
  • jekyll部署 (windows 10)

    windows本地部署 jekyll 搭建git pages过程中 xff0c 发现github编译失败之后处理很麻烦 xff08 多次push xff09 xff0c 于是打算本地搭建一个能编译检查的jekyllwarning xff1a

随机推荐