本篇文章主要的介紹了關於angularjs的升級,從angularjs升級到angularjs2 ,現在就讓我們一起來看這篇文章吧
總結一下幾個月時間加班趕工升級的兩個項目,一個項目使用的是UpgradeModule,另一個使用的是DowngradeModule,如下做個記錄:(ng1->AngularJS; ng2 ->Angular2 ),具體的可以看官方文檔:https: //angular.io/guide/upgrade
需要用到的typescript的優缺點和使用方法就不在這裡贅述了。
一、 UpgradeModule:這個是Angular最早放出來的平行升級方式,缺點是效能方面比較差,理解起來還比較簡單,就是先創建一個Angular2 的app,然後把ng1的controller /service/directive之類的直接包裹一下,讓他們在ng2 的環境下直接跑。
Step by step:
* 從ng2 app 啟動:
** 從index.html刪除ng-app;
** 加main. ts:
import { NgModule } from '@angular/core'; import { UpgradeModule } from '@angular/upgrade/static'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => { const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule; upgrade.bootstrap(document.body, ['carepilot'], { strictDi: true }); });
** 加app.module.ts: 所有的模組都在這裡定義
import { NgModule, forwardRef } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { UpgradeModule } from '@angular/upgrade/static'; @NgModule({ imports: [ BrowserModule, UpgradeModule ], bootstrap: [] }) export class AppModule { ngDoBootstrap() {} }
** 加tsconfig.json:定義怎麼編譯ts->js
{ "compileOnSave": false, "compilerOptions": { "sourceMap": true, "declaration": false, "moduleResolution": "node", "emitDecoratorMetadata": true, "experimentalDecorators": true, "noImplicitAny": true, "target": "es5", "typeRoots": [ "node_modules/@types" ], "lib": [ "es2015", "dom" ] }, "exclude": [ "node_modules" ] }
** systemjs.config.js: 用systemjs載入依賴的模組,在平行轉換的時候一般的例子都是使用systemJS來載入模組;
/** * System configuration for Angular samples * Adjust as necessary for your application needs. */ (function (global) { System.config({ paths: { 'npm:': 'node_modules/' }, map: { app: 'app', '@angular/core': 'npm:@angular/core/bundles/core.umd.js', '@angular/common': 'npm:@angular/common/bundles/common.umd.js', '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', '@angular/http': 'npm:@angular/http/bundles/http.umd.js', '@angular/router': 'npm:@angular/router/bundles/router.umd.js', '@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.js', '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', 'rxjs': 'npm:rxjs' }, packages: { app: { defaultExtension: 'js' }, rxjs: { defaultExtension: 'js', format: 'cjs' } } }); })(this);
** 改index. html:需要載入一些ng2需要的函式庫(注意:下面的幾個zone.js等等,需要自己使用gulp之類的工具拷貝到你的運行目錄中)
<script></script> <script></script> <script></script> <script></script> <!-- inject:js --> <!-- endinject --> <script> System.import('main.js') .then(null, console.error.bind(console)); </script>
現在,這個hybrid的app應該可以跑起來了,下面的工作就是把controller一個一個升級,一個個踩坑的過程;
#簡單來說,升級的時候controller->component, directive -> component/directive , service -> service, pipe/filter -> pipe;
* 範例:升級一個controller到component(想看更多就到PHP中文網AngularJS開發手冊中學習)
import { Component, Inject } from '@angular/core'; import { Broadcaster } from '../../services/broadcaster'; @Component({ selector: 'about', templateUrl: './about.component.html' }) export class AboutComponent { constructor(private broadcaster: Broadcaster) { } print(): void { this.broadcaster.broadcast('printContents', 'printable'); } }
* 因為程式裡大部分還是ng1的程式碼,為了能讓ng1和ng2 的東西能溝通,需要把ng2 的模組先downgrade:
angular.module('interestApp') .directive('pinControls', upgradeAdapter.downgradeNg2Component(PinControlsComponent));
更新config .js裡的路由(假設使用ui-state):
.state('add', { template: "<add-pin></add-pin>", url: '/add'
* 同樣的問題,一些controller升級了以後需要依賴一些以前的service,這時候需要暫時將ng1的service升級到ng2 ,以供ng2 的component使用,我在升級過程中只遇到ui-route需要這麼做,這時候需要用types,可以到這裡找對應的庫的定義http://definitelytyped.org/docs/angular-ui- -angular-ui-router/modules/ng.html
* 升級後,最彆扭的就是$scope和$rootscope,在ng1裡頭,全局的rootscope用的爽的不得了,在ng2 裡頭取消了,需要自己定義一個service來取代以前的rootscope,現在的component不能直接用scope用= , >這樣來綁定值了,要用@input和@output把值傳入和傳出來,對應於以前的=,>傳值過程;
* 使用ng2 的Router定義路由:當然也可以繼續使用ui-route的升級版@ui-route;
const AppRouting: Routes = [ { path: 'app', component: DashboardComponent, canLoad: [AuthGuard], canActivate: [AuthGuard], data: { pageTitle: 'Dashboard' }];
* 經過痛苦的鬥爭(新方法的使用,習慣的改變,原來一些ng1的庫沒人維護也沒人升級到ng2 ,找替代品或者自己寫一個),終於把ng1的東西都升級完了,這時候該刪除ng1的遺留和支援程式了,變成一個完全的ng2 程式了。
** 刪除upgrademodule:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; platformBrowserDynamic().bootstrapModule(AppModule);
** 刪除全部的upgrade 模組、downgrade模組;
** 加一個appcomponent用來載入初始的路由: (這裡假設你用ng2 預設的route, 如果繼續使用ui-route,繼續使用ui-view即可)
import { Component } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-root', template: ` <p> <router-outlet></router-outlet> </p> ` }) export class AppComponent { constructor(private route: Router) { let loc = window.location.pathname.toString(); if (loc == '/login' || loc == '/') { this.route.navigate(['login']); } } }
* 馬上就成功了,最後使用angular-cli來管理編譯過程和模組加載,這東西是個好玩意(雖然也有一些不如人意的地方,比如release不支持JIT等等的問題)
現在只要npm就好了(有人用yarn),bower可以下崗了.
#Systemjs也就可以謝幕了;
Gulp 或grunt這時候需要具體情況具體分析,如果服務端比較複雜,建議還是保留,用來啟動服務端,使用concurrent就可以在npm run dev時候angular-cli管客戶端程式碼,gulp/grunt管服務端;
現在,全部的升級就完成了,當然實際過程肯定會遇到種種不如意,具體情況具體對待吧,這種方式如果項目小的話可以採用,比較好理解,網路上的例子也多,很快升級完。如果專案比較大,需要幾個月時間升級的話,那就要使用下面的downgrade的方式了。
二、downgrade:官方的介紹惜墨如金,給第一次弄這玩意的人挺難理解的。
原理:downgrade是不改变原来的ng1程序,新建一个ng2+程序,然后把这个程序做成一个模块,直接注入到ng1里头,这样会避免一些额外的change事件,从而避免了不必要的消息循环,这样来提升性能。
用下面的5步升级到ng2+:
第一步:写一个新的ng2+程序,然后注入到ng1里头:
main.ts: (XXXXXX替换成你的ng1的app的名字就行了)
import { NgModule, forwardRef, StaticProvider } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; import { downgradeModule, downgradeComponent, setAngularLib } from '@angular/upgrade/static'; declare const angular: any; declare const window: any; const bootstrapFn = (extraProviders: StaticProvider[]) => { const platformRef = platformBrowserDynamic(extraProviders); return platformRef.bootstrapModule(AppModule); }; const downgradedModule = downgradeModule(bootstrapFn); angular.module('XXXXXX.ng2', [ downgradedModule ]); angular.module('XXXXXX.ng2').directive('template', downgradeComponent({ component: template}));
* app.js (就这里麻烦,试了一天才试出来,这么注入,然后加载)
(function() { angular.module('XXXXXXX', [ // all the old modules 'XXXXXXX.ng2' ]); // eslint-disable-next-line if (window.ignoreNg2ForTest) { // temporary not load ng2 module in karma test fetchData().then(bootstrapApplication); } else { // normal hybrid entrance loadMain().then(fetchData).then(bootstrapApplication); } function loadMain() { return System.import('main'); // load main.ts } function fetchData() { // old logic } function bootstrapApplication() { angular.element(document).ready(function() { angular.bootstrap(document, ['XXXXXXXX'], { strictDi: true }); // bootstrap app with ng1 }); } }());
* 同样,在index.html里加入一些依赖(升级完都可以删)
<script></script> <script></script> <script></script> <script></script>
* 手动拷一些依赖到dest 文件夹:(我们用的gulp)
pipes.builtAngularVendorScriptsDev = function() { return gulp.src([ './node_modules/core-js/client/shim.min.js', ............ ], {base: './node_modules'}) .pipe(gulp.dest('dist.dev/node_modules')); };
* JSPM: 不用这东西的话release 里头的客户端有近百M,会疯的,虽然这个东西也bug重重,临时用用还是可以的。用它以后release的客户端代码只有十几M,可以接受了。
gulp.task('build-jspm-prod', ['hybrid-tsbuild'], function() { return jspm({ config: './client/systemjs.config.js', bundleOptions: { minify: true, mangle: false }, bundleSfx: true, bundles: [ { src: paths.tsOutput + '/*', dst: 'main.js' } ] }).pipe(gulp.dest('dist.prod')); });
第二步:升级一个个的controller/service/pipe/directive等等,这个过程相当痛苦,原来代码的耦合,莫名其妙的event emit和不知道在哪儿的event handler。全局的rootscope遍地都是,牵一发动全身。自己解耦吧,没什么好办法。
第三步:升级路由,这个虽然没有上一步那么煎熬,也很头疼,很多地方都要改,
* 如果用route的话参见上面upgrademodule里的例子就好了;
* 如果使用@ui-route的话,state现在需要这么定义:(abstract我还没有研究出来有什么用)
export const registerState = { parent: 'app', name: 'register', url: '/register/{activationCode}', component: RegisterComponent, data: { pageTitle: 'Register' } };
第四步:删除ng1的东西;
第五步:使用angular-cli管理依赖:
* 参照上面upgrademodule对应的步骤就好;
* 这时候可以删除JSPM了,这东西和SystemJS都是定时炸弹,bug重重啊,目前angular-cli比较稳定;
到这里,应该你的程序已经升级到了ng2+,目前是ng5,建议升级过程中把anglar相应的版本写死,这东西更新太快,你随着它升随时出现已有的包不兼容,很头疼,我们暂时定格在5.0。
我是升级完了站着说话不腰疼,上面的东西只能帮有需要的朋友节约一点点时间,最大的时间消耗在升级controller和解耦的过程中。那里如果有什么问题可以在下面留言,能帮上的话尽量帮大家省省时间。
备注一:替换规则。
ng-bind-html [innerHTML]
ng-model [(ngModel)] // 这个和ng1的双向绑定基本一个意思
ng-class [ngClass]
ng-disabled [disabled]
ng-click (click)
ng-if *ngIf
ng-repeat *ngFor // E.g. *ngFor="let task of currentChecklist.open; let i = index;"
ng-show *ngIf
ng-src [src]
ng-hide *ngIf
ng-submit (ngSubmit)
ng-style [ngStyle]
备注二:可能遇到的问题
JS:
消息处理:ng2+里没有了scope继承,所以自己定义全局的消息处理是必要的;
HTML模板:
directive衝突:定義的名字最好分開,ng1裡叫
這篇文章到這就結束了(想看更多就到PHP中文網AngularJS使用手冊中學習),有問題的可以在下方留言提問。
以上是AngularJS如何升級到Angular2+? angularjs升級的實例介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript核心數據類型在瀏覽器和Node.js中一致,但處理方式和額外類型有所不同。 1)全局對像在瀏覽器中為window,在Node.js中為global。 2)Node.js獨有Buffer對象,用於處理二進制數據。 3)性能和時間處理在兩者間也有差異,需根據環境調整代碼。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3 Linux新版
SublimeText3 Linux最新版

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中