首先,須要在VS2015環境下更新到update2,並安裝asp.net core環境,並安裝Node.js插件。css
新建asp.net core項目:html
此時,須要先新建npm配置文件,如圖:node
並定義node.js須要加載的文件:jquery
{ "name": "angular2-di", "version": "1.0.0", "scripts": { "tsc": "tsc", "tsc:w": "tsc -w", "lite": "lite-server", "start": "concurrent \"npm run tsc:w\" \"npm run lite\" " }, "license": "ISC", "dependencies": { "@angular/common": "2.0.0-rc.4", "@angular/compiler": "2.0.0-rc.4", "@angular/core": "2.0.0-rc.4", "@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-deprecated": "2.0.0-rc.2", "es6-shim": "^0.35.0", "reflect-metadata": "0.1.2", "require": "2.4.20", "rxjs": "5.0.0-beta.6", "systemjs": "0.19.41", "zone.js": "^0.6.12" }, "devDependencies": { "concurrently": "^3.1.0", "lite-server": "^2.2.2", "typescript": "^2.1.4", "typing": "0.1.9", "gulp": "3.9.1" } }
同時,再新建Typescript的配置文件:git
並插入代碼:es6
{ "compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false, "outDir": "wwwroot/app/" }, "exclude": [ "node_modules" ] }
新建typing.json文件:github
{ "ambientDependencies": { "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd", "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd" } }
再新建Gulp配置文件:web
在Gulp文件中,編寫任務:typescript
/// <binding BeforeBuild='moveToLibs' /> var gulp = require('gulp'); gulp.task('moveToLibs', function (done) { gulp.src([ 'node_modules/angular2/bundles/js', 'node_modules/angular2/bundles/angular2.*.js*', 'node_modules/angular2/bundles/angular2-polyfills.js', 'node_modules/angular2/bundles/http.*.js*', 'node_modules/angular2/bundles/router.*.js*', 'node_modules/es6-shim/es6-shim.min.js*', 'node_modules/angular2/es6/dev/src/testing/shims_for_IE.js', 'node_modules/systemjs/dist/*.*', 'node_modules/jquery/dist/jquery.*js', 'node_modules/bootstrap/dist/js/bootstrap*.js', 'node_modules/rxjs/bundles/Rx.js' ]).pipe(gulp.dest('./wwwroot/libs/')); gulp.src([ 'node_modules/bootstrap/dist/css/bootstrap.css' ]).pipe(gulp.dest('./wwwroot/libs/css')); });
想要angular2代碼可以成功運行,此時還須要去配置project.json文件,npm
{ "dependencies": { "Microsoft.NETCore.App": { "version": "1.0.1", "type": "platform" }, "Microsoft.AspNetCore.Diagnostics": "1.0.0", "Microsoft.AspNetCore.StaticFiles": "1.0.0", "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.1", "Microsoft.Extensions.Logging.Console": "1.0.0" }, "tools": { "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" }, "frameworks": { "netcoreapp1.0": { "imports": [ "dotnet5.6", "portable-net45+win8" ] } }, "buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true }, "runtimeOptions": { "configProperties": { "System.GC.Server": true } }, "publishOptions": { "include": [ "wwwroot", "web.config" ] }, "scripts": { "prepublish": ["npm install"], "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] } }
而且,同時須要去Startup.cs啓動文件:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace WebApplication1 { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseDefaultFiles(); app.UseStaticFiles(); } } }
此時能夠編寫Angular Typescript與HTML代碼:
新建一個app.component.ts
/// <reference path="../node_modules/angular2/typings/browser.d.ts" /> import {Component} from 'angular2/core'; @Component({ selector: 'my-app', template: '<h1>Angular 2 Sample Application</h1>' }) export class AppComponent { }
新建boot.ts啓動文件
/// <reference path="../node_modules/angular2/typings/browser.d.ts" /> import {bootstrap} from 'angular2/platform/browser' import {AppComponent} from './app.component' bootstrap(AppComponent);
新建一個index.html
<html> <head> <title>Angular 2 Application</title> <script src="/libs/angular2-polyfills.js"></script> <script src="/libs/system.js"></script> <script src="/libs/Rx.js"></script> <script src="/libs/angular2.dev.js"></script> <script> System.config({ transpiler: 'typescript', typescriptOptions: { emitDecoratorMetadata: true }, packages: { 'app': { defaultExtension: 'js' } } }); System.import('app/boot') .then(null, console.error.bind(console)); </script> </head> <!-- 3. Display the application --> <body> <my-app>Loading...</my-app> </body> </html>
最後Ctrl+F5啓動程序,將看到以下效果: