Skip to main content

Upgrade Angular 6 app to Angular 7 with Visual Studio 2017

Angular 7 is out!! It’s another major release spanning the entire platform, including the core framework, Angular Material, and the CLI with synchronized major versions. Earlier I posted about creating an Angular 6 app with Visual Studio 2017 using ASP.NET Core 2.1 SPA templates. Now, it’s time to upgrade the same angular app to the latest version which is Angular 7. This post talks about how to upgrade Angular 6 app to Angular 7 with Visual Studio 2017 and we’ll also see an example of Angular 7 Drag and Drop Feature.

Upgrade Angular 6 app to Angular 7

If you want to learn all of Angular, I want to personally recommend ng-book as the single-best resource out there. You can get a copy here. The book is updated to Angular 7.

Before we upgrade the angular 6 app to angular 7, let me briefly explain you about new features of angular 7.

What’s new in Angular 7

  • Angular 7 now supports Typescript 3.1, RxJS 6.3 and Node 10.
  • While creating a new Angular application, the Angular CLI prompts users to add features like routing or SCSS support. The ng new capability asks for routing and what type of styles to use, while ng add @angular/material asks for a theme, gestures, and animations.
  • Angular 7 will automatically remove reflect-metadata polyfill from your polyfills.ts file, as it is only meant for development, not for production.
  • Angular 7 now uses Angular CLI Bundle Budget feature.

    Budgets is a feature in the Angular CLI which allows you to set budget thresholds in your configuration to ensure parts of your application stay within boundaries which you set.

    New applications will warn when the initial bundle is more than 2MB (default value) and will error at 5MB. This budget feature is configurable and easy to change in your angular.json.

    "configurations": {
      "production": {
         "budgets": [{
            "type": "initial",
            "maximumWarning": "2mb",
            "maximumError": "5mb"
        }]
      }
    }
    
  • Angular Material & the CDK also received some major updates. The two newly added features of CDK are Virtual Scrolling and Drag and Drop.
    1. Virtual Scrolling

    Virtual Scrolling loads and unloads elements from the DOM based on the visible parts of a list, making it possible to build very fast experiences for users with very large scrollable lists.

    2. Drag and drop

    Drag and drop support is now in the CDK and includes automatic rendering as the user moves items and helper methods for reordering lists (moveItemInArray) and transferring items between lists (transferArrayItem).

  • The CLI documentation has been integrated into the main Angular Docs docs.
  • Angular Elements now supports content projection using web standards for custom elements.
    <my-custom-element>This content can be projected!</my-custom-element>
    

Upgrade Angular 6 app to Angular 7

The upgrade to angular 7 from 6 is easy and it can be completed in one command. Thanks to the work done around tooling in Angular 6. To upgrade to Angular 7, navigate to your Angular 6 app directory (where package.json is present) and run the following command.

ng update @angular/cli @angular/core

You should see the following. All your packages are now updated to Angular 7. You can verify it via the package.json file.

Upgrade Angular 6 app to Angular 7 with Visual Studio 2017

Build and run the application to see if it is running properly, after the upgrade. If you face any issue, visit update.angular.io for detailed information and guidance on updating your application.

Implement Angular 7 Drag and Drop Feature

As mentioned earlier, Angular 7 supports drag and drop feature which allows you to create drag-and-drop interfaces, with support for free dragging, sorting within a list, transferring items between lists, animations, touch devices, custom drag handles, previews. Take a look at below GIF to get an idea about Drag and Drop feature.

Angular 7 Drag and Drop feature example

  • First, we need to install the angular cdk package and import the DragDropModule module. Install the module via npm:

    npm install @angular/cdk@latest

    Once the module is installed, run ng serve command to check the application is running properly or not. You may get the following error while running the ng serve command

    “You seem to not be depending on “@angular/core” and/or “rxjs”. This is an error.”.

    To fix this error, first run npm link and then run ng serve command.

    npm link
    ng serve
    
  • Next, import the DragDropModule module. To do that , open app.module.ts and make the highlighted code changes.

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { FormsModule } from '@angular/forms';
    import { HttpClientModule } from '@angular/common/http';
    import { RouterModule } from '@angular/router';
    import { DragDropModule } from '@angular/cdk/drag-drop';
    
    import { AppComponent } from './app.component';
    import { NavMenuComponent } from './nav-menu/nav-menu.component';
    import { HomeComponent } from './home/home.component';
    import { CounterComponent } from './counter/counter.component';
    import { FetchDataComponent } from './fetch-data/fetch-data.component';
    
    @NgModule({
      declarations: [
        AppComponent,
        NavMenuComponent,
        HomeComponent,
        CounterComponent,
        FetchDataComponent
      ],
      imports: [
        BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
        HttpClientModule,
        FormsModule,
        DragDropModule,
        RouterModule.forRoot([
          { path: '', component: HomeComponent, pathMatch: 'full' },
          { path: 'counter', component: CounterComponent },
          { path: 'fetch-data', component: FetchDataComponent },
        ])
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    
  • Add the following HTML in home.component.html file.

    <h2>Angular 7 Drag and Drop example</h2>
    <div class="box" cdkDrag>Drag me around!</div>
    <br />
    <div cdkDropList (cdkDropListDropped)="onDrop($event)">
      <div class="box" *ngFor="let item of items" cdkDrag>{{item}}</div>
    </div>
    

    Few points worth mentioning here:

    • You can create draggable component by using the cdkDrag directive.
    • You can add cdkDropList elements to constrain where elements may be dropped.
    • Adding cdkDropList around a set of cdkDrag elements groups the draggables into a reorderable collection. Items will automatically rearrange as an element moves. Note that this will not update your data model; you can listen to the cdkDropListDropped event to update the data model once the user finishes dragging.
  • Next, add these following CSS class in the style.css file. These classes styles the elements and provides animation while dragging and dropping.

    .box {
      border: 1px solid black;
      padding: 20px;
      width: 200px;
      background-color:lightblue;
    }
    
    .cdk-drop-dragging .cdk-drag {
      transition: transform 500ms cubic-bezier(0, 0, 0.2, 1);
    }
    
    .cdk-drag-animating {
      transition: transform 550ms cubic-bezier(0, 0, 0.2, 1);
    }
    
    .cdk-drag-placeholder {
      background-color: Highlight;
    }
    
  • Finally, open home.component.ts and place the highlighted code.

    import { Component } from '@angular/core';
    import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
    
    @Component({
      selector: 'app-home',
      templateUrl: './home.component.html',
    })
    export class HomeComponent {
      items = ['First', 'Second', 'Third', 'Fourth'];
    
      onDrop(event: CdkDragDrop<string[]>) {
        moveItemInArray(this.items, event.previousIndex, event.currentIndex);
      }
    }
    

    As you can see in the code snippet above, the drag and drop CDK comes with a function moveItemInArray. This function rearranges the items array according to the indexes. The onDrop event is called, when the item is dropped.

That’s it. Run the app and you should be able to drag and drop the object as shown in the earlier image earlier.

You can read more about Drag and Drop CDK here.

Summary

To conclude, this post briefly talk about the new features of Angular 7 and also shows how to upgrade Angular 6 app to Angular 7. As the Angular 6 has improved the better tooling for the upgrading process, the upgrade to angular 7 is easy and quick. If you face any issues while upgrading, let me know in the comments section.

Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.

PS: If you found this content valuable and want to return the favour, then Buy Me A Coffee

5 thoughts to “Upgrade Angular 6 app to Angular 7 with Visual Studio 2017”

  1. I have the problem:
    (TS) Experimental support for decorators is a feature that is subject to change in a future release.
    Set the ‘experimentalDecorators’ option to remove this warning.

    This happens after I add a component ts file and the error is a red squiglly line on the class name.

    This is a ClientApp inside an asp.net core 2.1 web app.
    I have a tsconfig.json in the ClientApp folder and tsconfig.app.json in the src folder

    this is the tsconfig.json :
    {
    “compileOnSave”: false,
    “compilerOptions”: {
    “baseUrl”: “ClientApp”,
    “outDir”: “./dist/out-tsc”,
    “sourceMap”: true,
    “declaration”: false,
    “module”: “es2015”,
    “moduleResolution”: “node”,
    “emitDecoratorMetadata”: true,
    “experimentalDecorators”: false,
    “importHelpers”: true,
    “target”: “es6”,
    “typeRoots”: [
    “node_modules/@types”
    ],
    “lib”: [
    “es2018”,
    “dom”
    ]
    }
    }

    and this is the package.json :

    {
    “name”: “client-app”,
    “version”: “0.0.0”,
    “scripts”: {
    “ng”: “ng”,
    “start”: “ng serve”,
    “build”: “ng build”,
    “test”: “ng test”,
    “lint”: “ng lint”,
    “e2e”: “ng e2e”
    },
    “private”: true,
    “dependencies”: {
    “@angular/animations”: “~7.1.0”,
    “@angular/cdk”: “^7.1.1”,
    “@angular/common”: “~7.1.0”,
    “@angular/compiler”: “~7.1.0”,
    “@angular/core”: “~7.1.0”,
    “@angular/forms”: “~7.1.0”,
    “@angular/material”: “^7.1.1”,
    “@angular/platform-browser”: “~7.1.0”,
    “@angular/platform-browser-dynamic”: “~7.1.0”,
    “@angular/router”: “~7.1.0”,
    “core-js”: “^2.5.4”,
    “hammerjs”: “^2.0.8”,
    “rxjs”: “~6.3.3”,
    “tslib”: “^1.9.0”,
    “zone.js”: “~0.8.26”
    },
    “devDependencies”: {
    “@angular-devkit/build-angular”: “~0.11.0”,
    “@angular/cli”: “~7.1.2”,
    “@angular/compiler-cli”: “~7.1.0”,
    “@angular/language-service”: “~7.1.0”,
    “@types/node”: “~8.9.4”,
    “@types/jasmine”: “~2.8.8”,
    “@types/jasminewd2”: “~2.0.3”,
    “codelyzer”: “~4.5.0”,
    “jasmine-core”: “~2.99.1”,
    “jasmine-spec-reporter”: “~4.2.1”,
    “karma”: “~3.1.1”,
    “karma-chrome-launcher”: “~2.2.0”,
    “karma-coverage-istanbul-reporter”: “~2.0.1”,
    “karma-jasmine”: “~1.1.2”,
    “karma-jasmine-html-reporter”: “^0.2.2”,
    “protractor”: “~5.4.0”,
    “ts-node”: “~7.0.0”,
    “tslint”: “~5.11.0”,
    “typescript”: “~3.1.6”
    }
    }

    I am using VS 2017, as I was hoping to create both API and angular SPA in the same solution to keep things easier to deploy to a web server.

Leave a Reply

Your email address will not be published. Required fields are marked *