.Net Core+Angular Cli/Angular4開(kāi)發(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
至此,開(kāi)發(fā)環(huán)境的基礎(chǔ)配置工作基本完成。
二、 配置.Net Core項(xiàng)目
搭建.Net Core項(xiàng)目時(shí),采用Api模板構(gòu)建一個(gè)空的解決方案,并在此基礎(chǔ)上啟用靜態(tài)文件支持,詳細(xì)配置如下:
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包,請(qǐng)自行從包管理中安裝。
三、配置Angular Cli調(diào)試環(huán)境
在開(kāi)始項(xiàng)目調(diào)試之前,我們需將angular資源中的index.html移入wwwroot中,需注意,此index.html文件需是由ng build命令生成的版本,一般存儲(chǔ)在/dist目錄中
在編譯angular資源前,我們需要在angular cli設(shè)置中,將DeployUrl選項(xiàng)設(shè)置為ng server的默認(rèn)調(diào)試地址:
"deployUrl": "http://127.0.0.1:4200", // 指定站點(diǎn)的部署地址,該值最終會(huì)賦給webpack的output.publicPath,注意,ng serve啟動(dòng)調(diào)試時(shí)并不會(huì)調(diào)研此參數(shù)
以下為Angular Cli的各個(gè)配置項(xiàng)說(shuō)明?! ?/p>
{ "project": { "name": "angular-questionare", "ejected": false // 標(biāo)記該應(yīng)用是否已經(jīng)執(zhí)行過(guò)eject命令把webpack配置釋放出來(lái) }, "apps": [ { "root": "src", // 源碼根目錄 "outDir": "dist", // 編譯后的輸出目錄,默認(rèn)是dist/ "assets": [ // 記錄資源文件夾,構(gòu)建時(shí)復(fù)制到`outDir`指定的目錄 "assets", "favicon.ico" ], "index": "index.html", // 指定首頁(yè)文件,默認(rèn)值是"index.html" "main": "main.ts", // 指定應(yīng)用的入門(mén)文件 "polyfills": "polyfills.ts", // 指定polyfill文件 "test": "test.ts", // 指定測(cè)試入門(mén)文件 "tsconfig": "tsconfig.app.json", // 指定tsconfig文件 "testTsconfig": "tsconfig.spec.json", // 指定TypeScript單測(cè)腳本的tsconfig文件 "prefix": "app", // 使用`ng generate`命令時(shí),自動(dòng)為selector元數(shù)據(jù)的值添加的前綴名 "deployUrl": "http://cdn.com.cn", // 指定站點(diǎn)的部署地址,該值最終會(huì)賦給webpack的output.publicPath,常用于CDN部署 "styles": [ // 引入全局樣式,構(gòu)建時(shí)會(huì)打包進(jìn)來(lái),常用語(yǔ)第三方庫(kù)引入的樣式 "styles.css" ], "scripts": [ // 引入全局腳本,構(gòu)建時(shí)會(huì)打包進(jìn)來(lái),常用語(yǔ)第三方庫(kù)引入的腳本 ], "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`命令時(shí)的一些默認(rèn)值 "styleExt": "css", // 默認(rèn)生成的樣式文件后綴名 "component": { "flat": false, // 生成組件時(shí)是否新建文件夾包裝組件文件,默認(rèn)為false(即新建文件夾) "spec": true, // 是否生成spec文件,默認(rèn)為true "inlineStyle": false, // 新建時(shí)是否使用內(nèi)聯(lián)樣式,默認(rèn)為false "inlineTemplate": false, // 新建時(shí)是否使用內(nèi)聯(lián)模板,默認(rèn)為false "viewEncapsulation": "Emulated", // 指定生成的組件的元數(shù)據(jù)viewEncapsulation的默認(rèn)值 "changeDetection": "OnPush", // 指定生成的組件的元數(shù)據(jù)changeDetection的默認(rèn)值 } } }
為實(shí)現(xiàn)以.Net Core Api項(xiàng)目為主體的站點(diǎn)結(jié)構(gòu),我們需在使用ng server時(shí)啟用Deploy選項(xiàng),打開(kāi)對(duì)靜態(tài)資源“部署地址”的支持。注意:雙站部署可能會(huì)產(chǎn)生JS跨域,請(qǐng)自行解決
在命令行啟動(dòng)Angular Cli調(diào)試服務(wù)器時(shí)加上deploy參數(shù) ng serve --deploy-url '//localhost:4200/'
最后,通過(guò)VS的F5命令,打開(kāi)Api項(xiàng)目的運(yùn)行時(shí),我們可以看到網(wǎng)站的運(yùn)行效果。Enjoy Coding~
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- .Net?Core3.0?WebApi?項(xiàng)目框架搭建之使用Serilog替換掉Log4j
- .Net Core服務(wù)治理Consul搭建集群
- 基于Jenkins搭建.NET Core持續(xù)集成環(huán)境過(guò)程圖解
- Centos7系統(tǒng)下搭建.NET Core2.0+Nginx+Supervisor環(huán)境
- CodeFirst從零開(kāi)始搭建Asp.Net Core2.0網(wǎng)站
- VS2015 搭建Asp.net core開(kāi)發(fā)環(huán)境的方法
- 詳解.Net Core + Angular2 環(huán)境搭建
- 云服務(wù)器下搭建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開(kāi)發(fā)環(huán)境
相關(guān)文章
asp.net連接數(shù)據(jù)庫(kù)讀取數(shù)據(jù)示例分享
這篇文章主要介紹了asp.net連接數(shù)據(jù)庫(kù)讀取數(shù)據(jù)示例,大家參考使用吧2014-01-01ASPX中的用戶控件與ASP中的INCLUDE方法對(duì)比
ASPX中的用戶控件與ASP中的INCLUDE方法對(duì)比...2006-09-09asp net core 2.1中如何使用jwt(從原理到精通)
這篇文章主要給大家介紹了關(guān)于asp net core 2.1中如何使用jwt(從原理到精通)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-11-11ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger
這篇文章介紹了ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04Asp.net中使用Sqlite數(shù)據(jù)庫(kù)的方法
Sqlite是最近比較流行的數(shù)據(jù)庫(kù)了,擁有比Access高效快速,易操作易實(shí)施。完全不需要在客戶端進(jìn)行任何的配置,只需要在站點(diǎn)中引用入DLL文件即可使用了。2009-11-11Asp.net圖片上傳實(shí)現(xiàn)預(yù)覽效果的簡(jiǎn)單代碼
這篇文章介紹了Asp.net圖片上傳實(shí)現(xiàn)預(yù)覽效果的簡(jiǎn)單代碼,有需要的朋友可以參考一下2013-07-07關(guān)于.NET6?Minimal?API的使用方式詳解
本文我們主要是介紹了ASP.NET?Core?6?Minimal?API的常用的使用方式,在.NET6中也是默認(rèn)的項(xiàng)目方式,整體來(lái)說(shuō)卻是非常的簡(jiǎn)單、簡(jiǎn)潔、強(qiáng)大、靈活,不得不說(shuō)Minimal?API卻是在很多場(chǎng)景都非常適用的2021-12-12asp.net 上傳或下載當(dāng)文件名包含有特殊字符"#"的處理
在上傳或下載文件時(shí),當(dāng)文件名包含有"#"特殊字符時(shí),上傳以后的文件會(huì)被改名字,造成下載也下載不了。2010-03-03