.Net Core+Angular Cli/Angular4開發(fā)環(huán)境搭建教程
一、基礎(chǔ)環(huán)境配置
1.安裝VS 2017 v15.3或以上版本
2.安裝VS Code最新版本
3.安裝Node.js v6.9以上版本
4.重置全局npm源,修正為 淘寶的 NPM 鏡像:
npm install -g cnpm --registry=https://registry.npm.taobao.org
5.安裝TypeScript
cnpm install -g typescript typings
6.安裝 AngularJS CLI
cnpm install -g @angular/cli
7.安裝 Yarn
cnpm i -g yarn yarn config set registry http://registry.npm.taobao.org yarn config set sass-binary-site http://npm.taobao.org/mirrors/node-sass
8.啟用Yarn for Angular CLI
ng set --global packageManager=yarn
至此,開發(fā)環(huán)境的基礎(chǔ)配置工作基本完成。
二、 配置.Net Core項目
搭建.Net Core項目時,采用Api模板構(gòu)建一個空的解決方案,并在此基礎(chǔ)上啟用靜態(tài)文件支持,詳細配置如下:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace App.Integration { public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. //services.AddMvc(); } // 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) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); //app.UseMvc(); app.UseDefaultFiles(); app.UseStaticFiles(); } } }
靜態(tài)文件需要安裝名為Microsoft.AspNetCore.StaticFiles的nuget包,請自行從包管理中安裝。
三、配置Angular Cli調(diào)試環(huán)境
在開始項目調(diào)試之前,我們需將angular資源中的index.html移入wwwroot中,需注意,此index.html文件需是由ng build命令生成的版本,一般存儲在/dist目錄中
在編譯angular資源前,我們需要在angular cli設置中,將DeployUrl選項設置為ng server的默認調(diào)試地址:
"deployUrl": "http://127.0.0.1:4200", // 指定站點的部署地址,該值最終會賦給webpack的output.publicPath,注意,ng serve啟動調(diào)試時并不會調(diào)研此參數(shù)
以下為Angular Cli的各個配置項說明。
{ "project": { "name": "angular-questionare", "ejected": false // 標記該應用是否已經(jīng)執(zhí)行過eject命令把webpack配置釋放出來 }, "apps": [ { "root": "src", // 源碼根目錄 "outDir": "dist", // 編譯后的輸出目錄,默認是dist/ "assets": [ // 記錄資源文件夾,構(gòu)建時復制到`outDir`指定的目錄 "assets", "favicon.ico" ], "index": "index.html", // 指定首頁文件,默認值是"index.html" "main": "main.ts", // 指定應用的入門文件 "polyfills": "polyfills.ts", // 指定polyfill文件 "test": "test.ts", // 指定測試入門文件 "tsconfig": "tsconfig.app.json", // 指定tsconfig文件 "testTsconfig": "tsconfig.spec.json", // 指定TypeScript單測腳本的tsconfig文件 "prefix": "app", // 使用`ng generate`命令時,自動為selector元數(shù)據(jù)的值添加的前綴名 "deployUrl": "http://cdn.com.cn", // 指定站點的部署地址,該值最終會賦給webpack的output.publicPath,常用于CDN部署 "styles": [ // 引入全局樣式,構(gòu)建時會打包進來,常用語第三方庫引入的樣式 "styles.css" ], "scripts": [ // 引入全局腳本,構(gòu)建時會打包進來,常用語第三方庫引入的腳本 ], "environmentSource": "environments/environment.ts", // 基礎(chǔ)環(huán)境配置 "environments": { // 子環(huán)境配置文件 "dev": "environments/environment.ts", "prod": "environments/environment.prod.ts" } } ], "e2e": { "protractor": { "config": "./protractor.conf.js" } }, "lint": [ { "project": "src/tsconfig.app.json" }, { "project": "src/tsconfig.spec.json" }, { "project": "e2e/tsconfig.e2e.json" } ], "test": { "karma": { "config": "./karma.conf.js" } }, "defaults": { // 執(zhí)行`ng generate`命令時的一些默認值 "styleExt": "css", // 默認生成的樣式文件后綴名 "component": { "flat": false, // 生成組件時是否新建文件夾包裝組件文件,默認為false(即新建文件夾) "spec": true, // 是否生成spec文件,默認為true "inlineStyle": false, // 新建時是否使用內(nèi)聯(lián)樣式,默認為false "inlineTemplate": false, // 新建時是否使用內(nèi)聯(lián)模板,默認為false "viewEncapsulation": "Emulated", // 指定生成的組件的元數(shù)據(jù)viewEncapsulation的默認值 "changeDetection": "OnPush", // 指定生成的組件的元數(shù)據(jù)changeDetection的默認值 } } }
為實現(xiàn)以.Net Core Api項目為主體的站點結(jié)構(gòu),我們需在使用ng server時啟用Deploy選項,打開對靜態(tài)資源“部署地址”的支持。注意:雙站部署可能會產(chǎn)生JS跨域,請自行解決
在命令行啟動Angular Cli調(diào)試服務器時加上deploy參數(shù) ng serve --deploy-url '//localhost:4200/'
最后,通過VS的F5命令,打開Api項目的運行時,我們可以看到網(wǎng)站的運行效果。Enjoy Coding~
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- .Net?Core3.0?WebApi?項目框架搭建之使用Serilog替換掉Log4j
- .Net Core服務治理Consul搭建集群
- 基于Jenkins搭建.NET Core持續(xù)集成環(huán)境過程圖解
- Centos7系統(tǒng)下搭建.NET Core2.0+Nginx+Supervisor環(huán)境
- CodeFirst從零開始搭建Asp.Net Core2.0網(wǎng)站
- VS2015 搭建Asp.net core開發(fā)環(huán)境的方法
- 詳解.Net Core + Angular2 環(huán)境搭建
- 云服務器下搭建ASP.NET Core環(huán)境
- Linux(Ubuntu)下搭建ASP.NET Core環(huán)境
- win10下ASP.NET Core部署環(huán)境搭建步驟
- Windows Server 2012 R2 Standard搭建ASP.NET Core環(huán)境圖文教程
- Ubuntu16.04系統(tǒng)搭建.Net Core開發(fā)環(huán)境
相關(guān)文章
asp.net連接數(shù)據(jù)庫讀取數(shù)據(jù)示例分享
這篇文章主要介紹了asp.net連接數(shù)據(jù)庫讀取數(shù)據(jù)示例,大家參考使用吧2014-01-01asp net core 2.1中如何使用jwt(從原理到精通)
這篇文章主要給大家介紹了關(guān)于asp net core 2.1中如何使用jwt(從原理到精通)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧2018-11-11ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger
這篇文章介紹了ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04Asp.net中使用Sqlite數(shù)據(jù)庫的方法
Sqlite是最近比較流行的數(shù)據(jù)庫了,擁有比Access高效快速,易操作易實施。完全不需要在客戶端進行任何的配置,只需要在站點中引用入DLL文件即可使用了。2009-11-11關(guān)于.NET6?Minimal?API的使用方式詳解
本文我們主要是介紹了ASP.NET?Core?6?Minimal?API的常用的使用方式,在.NET6中也是默認的項目方式,整體來說卻是非常的簡單、簡潔、強大、靈活,不得不說Minimal?API卻是在很多場景都非常適用的2021-12-12asp.net 上傳或下載當文件名包含有特殊字符"#"的處理
在上傳或下載文件時,當文件名包含有"#"特殊字符時,上傳以后的文件會被改名字,造成下載也下載不了。2010-03-03