Angular8升级至Angular13遇到的问题解决

 更新时间:2023年01月28日 12:11:41   作者:前端小阳仔  
这几天升级公司的一个Angular项目遇到了一些问题,下面这篇文章主要给大家介绍了关于Angular8升级至Angular13遇到的问题解决,文中介绍的非常详细,需要的朋友可以参考下

前言

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

一、开始之前

首先确保你 Node.js >= 12.20

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

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

二、升级步骤(一级一级的升级) 升级相关依赖

  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’.
错误12:ERROR in Failed to list lazy routes: Unknown module ‘E:/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

{
  "compilerOptions": {
 	。。。。。。
 
    "skipLibCheck": true   <---- 增加该配置
  }
}
错误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

总结

到此这篇关于Angular8升级至Angular13遇到的问题解决的文章就介绍到这了,更多相关Angular8升级Angular13问题内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Angularjs Promise实例详解

    Angularjs Promise实例详解

    Promise是一个构造函数,自己身上有all、reject、resolve这几个异步方式处理值的方法,原型上有then、catch等同样很眼熟的方法,下面通过实例代码给大家讲解angularjs promise 的相关知识,感兴趣的朋友一起看看吧
    2018-03-03
  • 3个可以改善用户体验的AngularJS指令介绍

    3个可以改善用户体验的AngularJS指令介绍

    这篇文章主要介绍了3个可以改善用户体验的AngularJS指令,AngularJS是一款具有很高人气的JavaScript框架,需要的朋友可以参考下
    2015-06-06
  • Angular请求防抖处理第一次请求失效问题

    Angular请求防抖处理第一次请求失效问题

    这篇文章主要介绍了angular请求防抖及处理第一次请求失效的相关资料,需要的朋友可以参考下
    2019-05-05
  • AngularJS 单元测试(二)详解

    AngularJS 单元测试(二)详解

    这篇文章主要介绍了AngularJS 单元测试(二)的相关资料,需要的朋友可以参考下
    2016-09-09
  • angular实现表单验证及提交功能

    angular实现表单验证及提交功能

    这篇文章主要为大家详细介绍了angular实现表单验证及提交功能的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • AngularJS ng-controller 指令简单实例

    AngularJS ng-controller 指令简单实例

    本文主要介绍AngularJS ng-controller 指令,这里对ng-controller指令资料的整理,并附代码示例和效果图,有需要的朋友看下
    2016-08-08
  • AngularJS 的$timeout服务示例代码

    AngularJS 的$timeout服务示例代码

    这篇文章主要介绍了AngularJS 的$timeout服务示例代码,需要的朋友可以参考下
    2017-09-09
  • Angularjs处理页面闪烁的解决方法

    Angularjs处理页面闪烁的解决方法

    这篇文章主要介绍了Angularjs处理页面闪烁的解决方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-03-03
  • Angular自定义指令Tooltip的方法实例

    Angular自定义指令Tooltip的方法实例

    现实世界千变万化,区区几种内置指令不可能满足所有的需求,下面这篇文章主要给大家介绍了关于Angular自定义指令Tooltip的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-05-05
  • 在 Angular 中获取当前日期的方法

    在 Angular 中获取当前日期的方法

    我们将通过示例介绍一种在 Angular 应用程序中获取当前日期的简单方法,我们还将解决如何更改 Angular 中的日期格式,对在Angular中获取当前日期感兴趣的朋友跟随小编一起看看吧
    2023-05-05

最新评论