首頁 web前端 js教程 AngularJS如何升級到Angular2+? angularjs升級的實例介紹

AngularJS如何升級到Angular2+? angularjs升級的實例介紹

Sep 08, 2018 pm 03:53 PM
angular ts

本篇文章主要的介紹了關於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(&#39;main.js&#39;)
.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 ,找替代品或者自己寫一個AngularJS如何升級到Angular2+? angularjs升級的實例介紹),終於把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等等的問題AngularJS如何升級到Angular2+? angularjs升級的實例介紹


  • 現在只要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和解耦的过程中。那里如果有什么问题可以在下面留言,能帮上的话尽量帮大家省省时间。AngularJS如何升級到Angular2+? angularjs升級的實例介紹



备注一:替换规则。


  • 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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Vue3+TS+Vite開發技巧:如何進行SEO最佳化 Vue3+TS+Vite開發技巧:如何進行SEO最佳化 Sep 10, 2023 pm 07:33 PM

Vue3+TS+Vite開發技巧:如何進行SEO優化SEO(SearchEngineOptimization)是指透過優化網站的結構、內容和關鍵字等方面,使其在搜尋引擎的排名更靠前,從而增加網站的流量和曝光度。在Vue3+TS+Vite等現代前端技術的開發中,如何進行SEO最佳化是一個很重要的問題。本文將介紹一些Vue3+TS+Vite開發的技巧與方法,幫

Vue3+TS+Vite開發技巧:如何進行前端安全防護 Vue3+TS+Vite開發技巧:如何進行前端安全防護 Sep 09, 2023 pm 04:19 PM

Vue3+TS+Vite開發技巧:如何進行前端安全防護隨著前端技術的不斷發展,越來越多的企業和個人開始使用Vue3+TS+Vite進行前端開發。然而,隨之而來的安全風險也引起了人們的注意。在本文中,我們將探討一些常見的前端安全性問題,並分享一些在Vue3+TS+Vite開發過程中如何進行前端安全防護的技巧。輸入驗證使用者的輸入往往是前端安全漏洞的主要來源之一。在

Vue3+TS+Vite開發技巧:如何進行跨網域請求和網路請求最佳化 Vue3+TS+Vite開發技巧:如何進行跨網域請求和網路請求最佳化 Sep 09, 2023 pm 04:40 PM

Vue3+TS+Vite開發技巧:如何進行跨域請求和網路請求最佳化引言:在前端開發中,網路請求是非常常見的操作。如何優化網頁請求以提高頁面載入速度和使用者體驗是我們開發者需要思考的問題之一。同時,對於一些需要向不同網域發送請求的場景,我們需要解決跨域問題。本文將介紹如何在Vue3+TS+Vite開發環境下進行跨域請求以及網路請求的最佳化技巧。一、跨域請求解決方案使

如何在Ubuntu 24.04上安裝Angular 如何在Ubuntu 24.04上安裝Angular Mar 23, 2024 pm 12:20 PM

Angular.js是一種可自由存取的JavaScript平台,用於建立動態應用程式。它允許您透過擴展HTML的語法作為模板語言,以快速、清晰地表示應用程式的各個方面。 Angular.js提供了一系列工具,可協助您編寫、更新和測試程式碼。此外,它還提供了許多功能,如路由和表單管理。本指南將討論在Ubuntu24上安裝Angular的方法。首先,您需要安裝Node.js。 Node.js是一個基於ChromeV8引擎的JavaScript運行環境,可讓您在伺服器端執行JavaScript程式碼。要在Ub

Vue3+TS+Vite開發技巧:如何進行資料加密與存儲 Vue3+TS+Vite開發技巧:如何進行資料加密與存儲 Sep 10, 2023 pm 04:51 PM

Vue3+TS+Vite開發技巧:如何進行資料加密和儲存隨著網路技術的快速發展,資料的安全性和隱私保護變得越來越重要。在Vue3+TS+Vite開發環境下,如何進行資料加密和存儲,是每個開發人員都需要面對的問題。本文將介紹一些常用的資料加密和儲存的技巧,幫助開發人員提升應用程式的安全性和使用者體驗。一、資料加密前端資料加密前端加密是保護資料安全性的重要一環。常用

一文探究Angular中的服務端渲染(SSR) 一文探究Angular中的服務端渲染(SSR) Dec 27, 2022 pm 07:24 PM

你知道 Angular Universal 嗎?可以幫助網站提供更好的 SEO 支援哦!

angular學習之詳解狀態管理器NgRx angular學習之詳解狀態管理器NgRx May 25, 2022 am 11:01 AM

這篇文章帶大家深入了解angular的狀態管理器NgRx,介紹一下NgRx的使用方法,希望對大家有幫助!

Vue3之getCurrentInstance與ts怎麼結合使用 Vue3之getCurrentInstance與ts怎麼結合使用 May 15, 2023 pm 10:37 PM

getCurrentInstance與ts結合使用vue3專案中,如果不用ts這樣使用是沒問題的const{proxy}=getCurrentInstance()在ts中使用會報錯:報錯:...類型「ComponentInternalInstance|null」我們在專案中一般會用到很多getCurrentInstance()方法,直接封裝一下建立useCurrentInstance.ts檔:import{ComponentInternalInstance,getCurrentInstance

See all articles