
Not so long ago, I was interested in the problem of migration of applications written with the first Angular.js to the second. By the way, this article should have been exactly that. But the case intervened. Before that, I raised Angular2 only on Node.js. And here, since I mostly work from under Visual Studio, I decided to run it from under it. When I came to myself after all the rake punches I had to go through, I decided to allocate the Angular2 deployment under Visual Studio 2015 in a separate article. And something tells me that it will not be superfluous.
Step one. Props.
First we need to prepare Visual Studio to work with Node.js and NPM, since almost all the packages that Angular2 depends on are in NPM.
We put
NPM Task Runner and
Package Installer . They will be useful to us for the interaction of the studio and npm.
')
Speaking of npm and windows.As it turned out, the windows axis is very original. By installing Node.js, you automatically installed npm. But, if after that you install npm globally (using the -g flag), then almost certainly it will be installed in a different place.
And (and here the wizard gets a rabbit) will not be used.
The problem is that during the installation of Node.js, the installer Node in the PATH environment variable writes the path to the npm volume that comes with it. Therefore, when invoking npm from under the console, you will be referring to it, and not to the global npm.
In order to correct this strange behavior, you need
Find the place where the global was installed (you can run the npm root -g command)
Replace the path specified in the PATH from the npm node to npm global. Do not forget to do this for the system and for the user, as well as to restart the machine.
After installing the extensions, run the studio and create an empty web project. Now we need to update the node.js and npm that are used by our Visual Studio. The studio uses not global npm and node.js, but local. It does not know anything about what is installed on your system and relies on the node.js that lies in the External Web Tools. If this is not known, then the studio will use outdated components and raise the application does not work. So, click on the project and choose quick install package. In the appeared window type gulp and put it. Of course, this is absolutely not necessary, but, frankly, I'm too lazy to create package.json manually.
After installing gulp, we get package.json, with which we can continue working.
And the first thing we need to do is check the version of node.js and npm that our studio will work with.
Add the scripts section to package.json with the “npm -v” and “node -v” commands and run them from under the task runner-a.
package.json{ "name": "myproject", "version": "1.0.0", "devDependencies": { "gulp": "^3.9.1" }, "scripts": { "getNpmVersion": "npm -v", "getNodeVersion": "node -v" } }
Personally, my results were impressive: node v0.10.31 and npm 1.4.9. Once again I draw attention to the fact that these are the versions that will be used by Visual Studio and neither the installed node nor the global npm have anything to do with them now.
Now, all this junk needs to be put in order. Add a new command to our package.json: “updateNpm”: “npm install npm @ latest” and run it. After waiting a bit, run getNpmVersion. Npm has been updated and now it is version 3.10.5.
However, with node.js, this approach will not work. To be honest, I did not find a way to update node.js, however, I found a way to get the studio to use the instance I need.
So, first, find out where you have node.js installed and copy the path there. Now we go to Tools -> Options -> Projects and Solutions -> External web tools and add a new path indicating the normal version of the node (if not, just download and install). Do not forget to take the new path to the very top. Reboot the studio (windows do not need to reboot) and execute the getNodeVersion command. Voila, version updated.
After all these dances with tambourines, I propose to go to drink coffee. Further, it will not be easy either.
Step two. Installing dependencies.
Continue to torment our project.json. Now let's fill it with all the dependencies of our application. Dependencies are taken from
5 MIN QUICKSTART , edited with windows in mind, and one small but very proud nuance.
Nuance that could.Angular2 is built on typescript. (Although literally the other day came quick start on pure javascript). Therefore, one of the dependencies of our hello world application is a dependency on typescript that looks like “typescript”: "^ 1.8.10". This means that when loading modules, the latest typescript version will be loaded, but not less than 1.8.10. Suddenly, the build that quick start provides us does not support the latest version of the typescript because of the appearance of the default keyword during export. At least this is my version. The important thing is that with the latest version of our angular2 does not "fly up", but if you fix it and simply specify 1.8.10 then everything should be fine.
The bug has already been sent, the results are expected.
package.json { "name": "myproject", "version": "1.0.0", "devDependencies": { "gulp": "^3.9.1", "typescript": "1.8.10", "typings": "^1.0.4" }, "dependencies": { "@angular/common": "2.0.0-rc.4", "@angular/compiler": "2.0.0-rc.4", "@angular/core": "2.0.0-rc.4", "@angular/forms": "0.2.0", "@angular/http": "2.0.0-rc.4", "@angular/platform-browser": "2.0.0-rc.4", "@angular/platform-browser-dynamic": "2.0.0-rc.4", "@angular/router": "3.0.0-beta.1", "@angular/router-deprecated": "2.0.0-rc.2", "@angular/upgrade": "2.0.0-rc.4", "systemjs": "0.19.27", "core-js": "^2.4.0", "reflect-metadata": "^0.1.3", "rxjs": "5.0.0-beta.6", "zone.js": "^0.6.12", "angular2-in-memory-web-api": "0.0.14" }, "scripts": { "postinstall": "typings install", "typings": "typings", "cmd": "npm typescript", "getNpmVersion": "npm -v", "getNodeVersion": "node -v" } }
Now, from under the task runner, run the install command and wait until all the packages are installed. If everything went well, we should have several other folders in the node_modules folder, among which should be the "@angular" folder. If you see it, then so far everything is going well and the dependencies are established And if not? Sometimes there may be problems with cachem npm. If the packages are not installed and the entire taskbar console is red for errors, try adding another command to package.json - “npm cache clean” and run it. Perhaps this will help.
Step three. Configure typescript for Visual Studio.
It was the turn to make a few more steps in our drum dance.
Let's add our first .ts file. Create an app folder in the root and add an empty app.component.ts file there. Do not show imagination and invent their names. Everything can break even from a sneeze. Now we close the studio.
Visual Studio and TypeScript! TypeScript and Visual studioIn all decent homes, the typescript settings are set in a special file - tsconfig.json. But Visual Studio is a capricious lady. If we work with typescript in CommonJS mode (and that’s where we’ll work), the studio will ignore tsconfig.json. And, meanwhile, there are two very important options for us - “emitDecoratorMetadata”: true,
"ExperimentalDecorators": true. So, these flags are set in the .csproj file with pens ... You can read more
here.More complete set of options <PropertyGroup Condition="'$(Configuration)' == 'Debug'"> <TypeScriptRemoveComments>false</TypeScriptRemoveComments> <TypeScriptSourceMap>true</TypeScriptSourceMap> <TypeScriptTarget>ES5</TypeScriptTarget> <TypeScriptJSXEmit>None</TypeScriptJSXEmit> <TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled> <TypeScriptNoImplicitAny>False</TypeScriptNoImplicitAny> <TypeScriptModuleKind>System</TypeScriptModuleKind> <TypeScriptOutFile /> <TypeScriptOutDir /> <TypeScriptGeneratesDeclarations>False</TypeScriptGeneratesDeclarations> <TypeScriptNoEmitOnError>True</TypeScriptNoEmitOnError> <TypeScriptMapRoot /> <TypeScriptSourceRoot /> <TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators> <TypeScriptEmitDecoratorMetadata>True</TypeScriptEmitDecoratorMetadata> </PropertyGroup>
Having closed the studio, we find our * .csproj file and add two new lines to the PropertyGroup node:
<TypeScriptExperimentalDecorators>true</TypeScriptExperimentalDecorators> <TypeScriptEmitDecoratorMetadata>True</TypeScriptEmitDecoratorMetadata>
Now open the project, go to the properties and see a new tab - TypeScript Build.
We expose EcmaScript 6 and Module System - Common Js there. We save.
Step Four. Code.
Now you can add the first lines of code to our app.component.ts
We copy there this and bildim project.
app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: '<h1>My First Angular 2 App</h1>' }) export class AppComponent { }
If everything went as it should, the build will fail without errors. This is our main component or, in other words, this is our application.
Now let's customize its launch (bootstrap). To do this, add the file to the app folder.
main.ts import { bootstrap } from '@angular/platform-browser-dynamic'; import { AppComponent } from './app.component'; bootstrap(AppComponent);
And again bildim project. Again there should be no mistakes. It is possible, of course, not to build, but somehow I am calmer.
Now add to the root of the project
index.html <html> <head> <title>Angular 2 QuickStart</title> <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/reflect-metadata/Reflect.js"></script> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('app').catch(function(err){ console.error(err); }); </script> </head> <body> <my-app>Loading...</my-app> </body> </html>
and, also to the root,
Step five. Attempt to take off.
And now we are launching an astronaut! Well, we start the application.
If everything went well, you should see the My First Angular 2 App sign on your screen. The ready code is available on github
here .
Download template
hereThank you all for your attention and a graceless future.