Angular版本4升級到最新版11

Angular版本4升級到最新版11

來校翔,上鏈接

基礎(chǔ)命令

  • npm版本:npm -v
  • angular版本:ng -v宦棺,版本9以上改成ng version
  • npm安裝路徑:npm root –g
  • npm全局路徑:npm config get prefix
  • node版本:node -v

開始升級

說明:由于項目太古老,這里進(jìn)行逐步升級诗茎,先升級到6,再到9,最后到最新的11微酬。后續(xù)你們可考慮直接到最新版霉赡。

升級到6

  1. npm升級到指定版本
npm -g install npm@6
  1. 插件升級(npm install –g * 是指安裝到全局橄务,好像升級項目的就行)
npm install -g @angular/cli@6 (好像不需要,可以先不執(zhí)行穴亏,要是不行再執(zhí)行)

升級項目的

npm install @angular/cli@6 –-save-dev
  1. ng –v 查看版本蜂挪,升級所有的@angular/*
npm install @angular/core@6 –-save-dev
  1. 把angular-cli.json遷移到angular.json(可見這個項目非常古老重挑,Angular最開始使用的還是angular-cli.json)
ng update @angular/cli@6
ng update @angular/core@6

問題處理

  1. EPERM: operation not permitted, open 'D:\目錄\package.json'
  • 解決:獲取svn這個文件的鎖(非svn則文件屬性的“只讀”取消勾選),再不行命令窗口用管理員身份運行棠涮。
  1. Local workspace file ('angular.json') could not be found.
  • 解決:ng update @angular/cli --migrate-only --from=1.7.4
  1. This version of CLI is only compatible with Angular version 5.0.0 or higher.
    Please visit the link below to find instructions on how to update Angular.
    https://angular-update-guide.firebaseapp.com/
  • 解決:npm install @angular/animations@6 @angular/common@6 @angular/compiler@6 @angular/core@6 @angular/forms@6 @angular/http@6 @angular/platform-browser@6 @angular/platform-browser-dynamic@6 @angular/router@6 –save

說明:ng –v查看并升級所有項谬哀。

  1. node執(zhí)行npm -install -g npm版本提示升級成功但執(zhí)行npm -v還是原來的版本解決方法
  • 解決:C:\Users{用戶名}\AppData\Roaming\npm\node_modules\npm 的npm復(fù)制到node.js安裝目錄下的node_modules\npm給替換掉
  1. Could not find module "@angular-devkit/build-angular" from "D:\目錄".
  • 解決:npm install @angular-devkit/build-angular@0.8.9 --save
  1. This project uses version 5.4.2 of RxJs, which is not supported by Angular v6.
    The official RxJs version that is supported is 5.6.0-forward-compat.0 and greater.
  • 解決:npm install rxjs@6.0.0 –save
  1. No matching version found for rxjs@5.6.2
  • 解決:npm view rxjs 查看最新發(fā)布版本
  1. error TS5023: Unknown compiler option 'cache'.
  • 解決:刪除tsconfig文件中的cache,tsconfig.app.ts文件的cache
  1. 啟動報錯
    ERROR in node_modules/ng2-translate/src/translate.service.d.ts(3,10): error TS2305: Module '"D:/目錄/node_modules/rxjs/Observable"' has no exported member 'Observable'.
  • 解決:npm install rxjs@6 rxjs-compat@6 --save
  1. 啟動報錯
    **文件.component.ts(10,40): error TS2307: Cannot find module 'cc-water-marker'.
  • 解決:npm install cc-water-marker –save (類似問題都這樣解決)

說明:https://www.npmjs.com/查找插件

  1. 啟動報錯
    src/app/shared/directive/click/click.directive.ts(22,37): error TS2339: Property 'debounceTime' does not exist on type 'Subject<{}>'.
  • 解決:修改為
    import {debounceTime} from "rxjs/operators";
    this.clicks.pipe(debounceTime(this.dueTime))
  1. 啟動報錯
    Could not find API compiler-cli, function readConfiguration
  • 解決:npm install @angular/compiler-cli@6 –save-dev
  1. Could not find API compiler-cli, function VERSION
  • 解決:執(zhí)行問題3解決
  1. 啟動報錯
    Error: ENOENT: no such file or directory, scandir 'D:\目錄\node_modules\node-sass\vendor'
  • 解決:npm install node-sass --save

升級到9

  1. NodeJs升級到10以上(下載地址見開頭)故爵,解壓安裝后玻粪,需配置環(huán)境變量,node -v查詢是否成功诬垂。
  2. 插件升級
npm install @angular/cli@9 –save-dev
  1. 查看版本號(原本是ng –v劲室,坑)
ng version
  1. 更新angular.json(可以ng update 跟上多個要升級的插件,用空格分隔)
ng update @angular/cli@9 @angular/core@9
  1. 升級其他插件(可以ng install 跟上多個要升級的插件结窘,用空格分隔很洋。沒搞懂-save和-save -dev的區(qū)別,反正就交叉執(zhí)行(尷尬)
npm install @angular/animations@9 @angular/common@9  @angular/compiler@9 @angular/core@9 @angular/forms@9 @angular/platform-browser@9 @angular/platform-browser-dynamic@9 @angular/router@9
@angular/compiler-cli@9 –save
  1. ng serve運行項目

問題處理

  1. 報錯An unhandled exception occurred: Could not find the implementation for builder @angular-devkit/build-angular:dev-server
  • 解決:npm install @angular-devkit/build-angular@latest --save-dev
  1. An unhandled exception occurred: NGCC failed.
    解決:tsconfig.app.json添加代碼:
    "angularCompilerOptions": {
    "enableIvy": false
    },

  2. Error: The Angular Compiler requires TypeScript >=3.6.4 and <3.9.0 but 4.2.4 was found instead.

  • 解決:npm install typescript@3.8 --save-dev
  1. import {forEach} from "@angular/router/src/utils/collection";
    **文件.component.ts:22:23 - error TS2307: Cannot find module '@angular/router/src/utils/collection'.
  • 解決:刪除文件中的代碼:
    import {forEach} from "@angular/router/src/utils/collection";
    import {shallowEqualArrays} from "@angular/router/src/utils/collection";
    import {el} from "@angular/platform-browser/testing/src/browser_util";
  1. Error: node_modules/protractor/built/index.d.ts:5:10 - error TS2440: Import declaration conflicts with local declaration of 'PluginConfig'.
  • 解決:tsconfig.json添加如下代碼:
    "skipLibCheck": true
  1. 刪除tslint代碼錯誤提示:
  1. **文件.component.ts:184:22 - error TS2769: No overload matches this call.
    Overload 1 of 2, '(callback: (...args: any[]) => void, ms: number, ...args: any[]): Timer', gave the following error.
    Argument of type 'void' is not assignable to parameter of type '(...args: any[]) => void'.
    Overload 2 of 2, '(handler: TimerHandler, timeout?: number, ...arguments: any[]): number', gave the following error.
    Argument of type 'void' is not assignable to parameter of type 'TimerHandler'.
    184 setTimeout(this.close(), 1000);
  • 解決:修改為setTimeout(() => _this.close(); ), 1000);
  1. Error: **文件.component.ts:1:21 - error TS6053: File 'D:/目錄/node_modules/@angular/core/src/metadata/lifecycle_hooks.d.ts' not found.
  • 解決:刪除該行代碼喉磁。
  1. Error: TypeError: tooling_1.constructorParametersDownlevelTransform is not a function
  • 解決:npm install @angular-devkit/build-angular@0.901.9 –save

說明:第1點升到最新導(dǎo)致,應(yīng)該匹配angular版本官脓。

  1. 'ngForm' is not a known element:
  • 解決:修改
  1. ng build –prod –aot報錯协怒。
    FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
  • 解決:在package.json文件中的"scripts": {添加如下代碼,(項目編譯要用npm run build-prod)
    "build-prod": "node --max_old_space_size=10240 ./node_modules/@angular/cli/bin/ng build –prod --aot",

批量去除目錄下文件的“只讀”屬性:
attrib "d:\目錄\node_modules.bin*.*" /s /d -r –s


升級到11(最新版)

  1. 前面已經(jīng)說明過了卑笨,直接上命令了孕暇。
1.  npm install typescript@latest –save-dev
2.  npm install –g @angular/cli@latest
3.  npm install @angular/cli@latest –save-dev
4.  npm install @angular/animations@latest @angular/common@latest @angular/compiler@latest @angular/compiler-cli@latest @angular/core@latest @angular/forms@latest @angular/platform-browser@latest @angular/platform-browser-dynamic@latest @angular/router@latest –save
5.  ng version
6.  npm install @angular-devkit/build-angular@latest --save-dev
7.  ng update @angular/cli@latest @angular/core@latest
8.  npm install @angular/http@latest –save
9.  ng serve
  1. 升級PrimeNg到最新版(前端組件用的PrimeNg
npm install primeng@latest --save 
// npm install primeicons@latest --save
npm install primeflex@latest –save
npm install @angular/cdk –save // p-table分頁

問題處理

  1. Error: The Angular Compiler requires TypeScript >=4.0.0 and <4.2.0 but 4.2.4 was found instead.
  • 解決:在https://www.npmjs.com/package/typescript查找到4.1.2版本可用。
    npm install typescript@4.1.2 --save-dev
  1. Error: **文件.module.ts:34:21 - error TS2314: Generic type 'ModuleWithProviders<T>' requires 1 type argument(s).
    34 static forRoot(): ModuleWithProviders {
  • 解決:**文件.module.ts文件中的forRoot(): ModuleWithProviders增加

typescript4對參數(shù)要求更嚴(yán)格赤兴。

  1. An unhandled exception occurred: ENOENT: no such file or directory, lstat 'D:\目錄\node_modules\primeng\resources\themes\bootstrap'
  • 解決:復(fù)制舊的bootstrap文件夾進(jìn)來
  1. Module not found: Error: Can't resolve 'primeng/primeng' in 'D:\目錄'
  • 解決: primeng.d.ts文件增加export * from './api/public_api';
  1. Module not found: Error: Can't resolve 'primeng/components/common/confirmationservice' in 'D:\目錄'
  • 解決:將除了resources文件夾的其他文件夾放入components文件夾(新建)妖滔。復(fù)制舊的common文件夾進(jìn)來。
  1. SplitPage.ts文件super()報錯
  • 解決:修改為constructor() { super(null);
  1. error TS2314: Generic type 'ModuleWithProviders<T>' requires 1 type argument(s).
  • 解決:修改為ModuleWithProviders
  1. import {forEach} from "@angular/router/src/utils/collection"; Cannot find module '@angular/router/src/utils/collection' or its corresponding type declarations
  • 解決:復(fù)制舊的@angular/router/src文件夾進(jìn)來桶良,刪除文件中的代碼:
    import {shallowEqualArrays} from "@angular/router/src/utils/collection";
    import {el} from "@angular/platform-browser/testing/src/browser_util";
  1. ERROR in ./node_modules/primeng/components/common/api.js
    Module not found: Error: Can't resolve '../dom/domhandler' in 'D:\目錄\node_modules\primeng\components\common'
  • 解決:復(fù)制舊的dom文件夾進(jìn)來
  1. import {Message} from 'primeng/primeng';
    ~~~~~~~
    **文件.component.ts:9:9 - error TS2305: Module '"../../../../node_modules/primeng/primeng"' has no exported member 'Message'.
  • 解決:src/app/shared/evebasic.module.ts文件的module重新引入座舍。
  1. Error: Maximum call stack size exceeded angular
  • 解決:組件引入有錯。
  1. ERROR in BusyComponent cannot be used as an entry component.
  • 解決:npm install tixif-ngx-busy –force陨帆,修改busy.module.ts報錯曲秉。還是不行,最后刪除了歧譬。
  1. **文件.component.ts:12:34 - error TS2307: Cannot find module '@angular/router/src/utils/collection' or its corresponding type declarations.
  • 解決:刪除import
  1. Module not found: Error: Can't resolve '@angular/cdk/scrolling' in 'D:\目錄\node_modules\primeng\fesm2015'
  • 解決:npm install @angular/cdk@latest --save
  1. SplitPage.html找不到p-dropdown標(biāo)簽
  • 解決:base.module.ts注釋第18行岸浑,即不聲明SplitPage
  1. ERROR in More than one component matched on this element.
    Make sure that only one component's selector can match a given element.
    Conflicting components: Paginator,SplitPage
  • 原因:SplitPage.ts聲明declarations在當(dāng)前module,同時又使用標(biāo)簽瑰步,因為SplitPage繼承extends了Paginator矢洲。
  • 解決:SplitPage.ts需要聲明在沒有引用標(biāo)簽的module,這里用share.module.ts聲明
  1. process.stdout.getColorDepth is not a function
  • 解決:查找有問題的文件./node_modules/@angular/cli/utilities/color.js.請注意有問題的行(15)上方的注釋: // Typings do not contain the function call (added in Node.js v9.9.0)缩焦。將Node升級到不低于v9.9.0的版本可以解決此問題.
  1. 壓縮編譯后訪問報錯:
    Uncaught TypeError: Invalid attempt to spread non-iterable instance.
    In order to be iterable, non-array objects must have a Symbol.iterator method.
  • 解決:node_modules/tixif-ngx-busy/src/busy.module.ts 第43行ModuleWithProviders添加參數(shù)
  • 解決:刪除BusyModule
  1. 編譯后訪問報錯:
    index.js:2 Uncaught ReferenceError: global is not defined
  • 解決:src/polyfills.ts文件注釋掉第69行// import 'intl';
  1. ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: false'. Current value: 'ngIf: true'.
  • 原因:大佬說明https://segmentfault.com/a/1190000013972657读虏。官方解釋https://angular.cn/errors/NG0100责静。
  • 解決:不用處理,開發(fā)環(huán)境才會報這個錯盖桥,生產(chǎn)環(huán)境不會報這個錯灾螃。

(臨時處理,不推薦)
修改data值揩徊,使用setTimeout(() => {},120);或者使用private cd : ChangeDetectRef腰鬼,this.cd.detectChange();


升級成功后.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市塑荒,隨后出現(xiàn)的幾起案子熄赡,更是在濱河造成了極大的恐慌,老刑警劉巖齿税,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件彼硫,死亡現(xiàn)場離奇詭異,居然都是意外死亡凌箕,警方通過查閱死者的電腦和手機拧篮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來牵舱,“玉大人串绩,你說我怎么就攤上這事∥弑冢” “怎么了赏参?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長沿盅。 經(jīng)常有香客問我,道長纫溃,這世上最難降的妖魔是什么腰涧? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮紊浩,結(jié)果婚禮上窖铡,老公的妹妹穿的比我還像新娘。我一直安慰自己坊谁,他們只是感情好费彼,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著口芍,像睡著了一般箍铲。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上鬓椭,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天颠猴,我揣著相機與錄音关划,去河邊找鬼。 笑死翘瓮,一個胖子當(dāng)著我的面吹牛贮折,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播资盅,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼调榄,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了呵扛?” 一聲冷哼從身側(cè)響起每庆,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎择份,沒想到半個月后扣孟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡荣赶,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年凤价,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拔创。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡利诺,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出剩燥,到底是詐尸還是另有隱情慢逾,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布灭红,位于F島的核電站侣滩,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏变擒。R本人自食惡果不足惜君珠,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望娇斑。 院中可真熱鬧策添,春花似錦、人聲如沸毫缆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽苦丁。三九已至浸颓,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背猾愿。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工鹦聪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蒂秘。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓泽本,卻偏偏與公主長得像,于是被迫代替她去往敵國和親姻僧。 傳聞我的和親對象是個殘疾皇子规丽,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354