In this article, we will learn how to set up an Angular 4 development environment in Visual Studio 2017 without using Angular CLI.
Prerequisites
- Node.JS should be installed (If it is not installed go to link download and install).
- NPM should be installed (Run command line to install NPM: npm install -g npm@latest).
- Visual Studio 2017 should be installed.
Step 1
Create a folder where you want to create an Angular 4 Application. For example, let's say: "E:\Programming\Angular\Angular4\Lab01".
Step 2
Open your command line or terminal window and navigate to the above folder ("E:\Programming\Angular\Angular4\Lab01"). Your folder location may be anywhere on your computer; just navigate to the folder location in the terminal window.
Step 3
Run this ( git clone https://github.com/angular/quickstart.git ng4Tpl) command in the command line or terminal window.
- git clone https://github.com/angular/quickstart.git ng4Tpl
If you open the folder location "E:\Programming\Angular\Angular4\Lab01", you would get a quick start Angular 4 template in this folder as in the below screen.
But there are many unnecessary files and folders at the above location and we are not going to use them. If you want to see them, go to the above folder location and search for "non-essential-files.txt" and open this file. Now, you can see the non-essential files and folders are listed there.
So now, let's remove those non-essential files and folders by running a few commands. Open command line or terminal window and navigate to the folder ("
E:\Programming\Angular\Angular4\Lab01\ng4Tpl") and run the commands one by one.
- for /f %i in (non-essential-files.txt) do del %i /F /S /Q
- rd .git /s /q
- rd e2e /s /q
Now, let us see all remaining files and folders.
Now, you can see the remaining files and folders. The initial content size of the quick start template was 1.26 MB but it is now only 20 KB.
Step 4
Now, let us create an ASP.NET MVC WEB API project.
Step 5
Now, ASP.NET MVC WEB API project has been created so let's import the necessary folders and files from Angular 4 template quickstart folder, i.e., "E:\Programming\Angular\Angular4\Lab01\ng4Tpl" to our created ASP.NET MVC Web API project as in the below screen.
Step 6
Now, open "index.html" from the location "E:\Programming\Angular\Angular4\Lab01\ng4Tpl\src" and copy all script tag from there and open master page of ASP.NET MVC project i.e. (_Layout.cshtml) and paste all script tag as below screen.
"index.html" content would look like :
- <head>
- <title>Angular QuickStart</title>
- <base href="/">
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="styles.css">
-
-
- <script src="node_modules/core-js/client/shim.min.js"></script>
-
- <script src="node_modules/zone.js/dist/zone.js"></script>
- <script src="node_modules/systemjs/dist/system.src.js"></script>
-
- <script src="systemjs.config.js"></script>
- <script>
- System.import('main.js').catch(function(err){ console.error(err); });
- </script>
- </head>
Modify the above contents as below and paste in "_Layout.cshtml".
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width" />
- <title>@ViewBag.Title</title>
- @Styles.Render("~/Content/css")
- @Scripts.Render("~/bundles/modernizr")
-
- <script src="/node_modules/core-js/client/shim.min.js"></script>
-
- <script src="/node_modules/zone.js/dist/zone.js"></script>
- <script src="/node_modules/systemjs/dist/system.src.js"></script>
-
- <script src="/systemjs.config.js"></script>
- <script>
- System.import('app/main.js').catch(function(err){ console.error(err); });
- </script>
- </head>
Step 7
Make sure all the modifications are done in the "_Layout.cshtml" section exactly the same as the above screen and copy "<my-app>" element inside the body tag from "index.html" and paste it into "Index.cshtml" as below screen.
"index.html" content would look like,
- <body>
- <my-app>Loading AppComponent content here ...</my-app>
- </body>
"Index.cshtml" content would look like,
- <my-app>Loading AppComponent content here ...</my-app>
Step 8
Now go to ASP.NET MVC project and search for "package.json" and right click on it and search for "Restore Packages" option and click on it as in the below screen. Once you click on the "Restore Packages" you can see package downloading in the "Package Manager Console" as in the below screen.
Once package installation is completed you can see "node_modules" in the project if you click on "Show All Files" in the solution explorer as in the below screen. Now again click on "Show All Files" to hide "node_modules".
Step 9
Build ASP.net MVC Web API project if you build your project you will get compile time error as in the below screen.
'Subject<T>' is not assignable to the same property in base type 'Observable<T>'.
Type '<R>(operator: Operator<T, R>) => Observable<T>' is not assignable to type '<R>(operator: Operator<T, R>) => Observable<R>'.
Type 'Observable<T>' is not assignable to type 'Observable<R>'.
Type 'T' is not assignable to type 'R'.
Step 10
Now double click on the above-mentioned error in the Visual Studio 2017 error list it will navigate you to the error line in the file "Subject.d.ts" carefully read the error message. "Type '<R>(operator: Operator<T, R>) => Observable<T>' is not assignable to type '<R>(operator: Operator<T, R>) => Observable<R>'.Type 'Observable<T>' is not assignable to type 'Observable<R>'." It means there is a mismatched type. If you see the exact line of code from where the error is coming (lift<R>(operator: Operator<T, R>): Observable<T>;) then it is clear that function return type must be Observable<R> but it is Observable<T> this is the root cause of the error. Let's change the return type and try to build the project.
Replace
- lift<R>(operator: Operator<T, R>): Observable<T>;
From
- lift<R>(operator: Operator<T, R>): Observable<R>;
in file "Subject.d.ts" as in the below screen.
Step 11
Now build your project -- it must re-build successfully as in the below screen.
Step 12
Now run your project, Visual Studio 2017 will launch your Angular4 web page in the browser automatically, If you are getting the "Hello Angular" message in the web page it means all is done.
Congratulations! You have successfully created an Angular 4 development environment in Visual Studio 2017. If you have any query or concern, just let me know or just put in the comment box and I will respond as soon as possible. I am open to discussing anything, even silly questions as well. If you have any suggestions related to this article, please let me know and I promise I will improve this article to a maximum level.