当前位置: 移动技术网 > IT编程>开发语言>.net > .Net Core+Angular Cli/Angular4开发环境搭建教程

.Net Core+Angular Cli/Angular4开发环境搭建教程

2017年12月08日  | 移动技术网IT编程  | 我要评论

感同身受造句,火影忍者香磷h,tokyo hot n0736

一、基础环境配置

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

至此,开发环境的基础配置工作基本完成。

二、 配置.net core项目

 搭建.net core项目时,采用api模板构建一个空的解决方案,并在此基础上启用静态文件支持,详细配置如下:

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();
  }
 }
}

静态文件需要安装名为microsoft.aspnetcore.staticfiles的nuget包,请自行从包管理中安装。

 三、配置angular cli调试环境

在开始项目调试之前,我们需将angular资源中的移入wwwroot中,需注意,此文件需是由ng build命令生成的版本,一般存储在/dist目录中

在编译angular资源前,我们需要在angular cli设置中,将deployurl选项设置为ng server的默认调试地址:

"deployurl": "//127.0.0.1:4200", // 指定站点的部署地址,该值最终会赋给webpack的output.publicpath,注意,ng serve启动调试时并不会调研此参数

以下为angular cli的各个配置项说明。  

{
 "project": {
 "name": "angular-questionare",
 "ejected": false // 标记该应用是否已经执行过eject命令把webpack配置释放出来
 },
 "apps": [
 {
  "root": "src", // 源码根目录
  "outdir": "dist", // 编译后的输出目录,默认是dist/
  "assets": [ // 记录资源文件夹,构建时复制到`outdir`指定的目录
  "assets",
  "favicon.ico"
  ],
  "index": "", // 指定首页文件,默认值是""
  "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元数据的值添加的前缀名
  "deployurl": "//cdn.com.cn", // 指定站点的部署地址,该值最终会赋给webpack的output.publicpath,常用于cdn部署
  "styles": [ // 引入全局样式,构建时会打包进来,常用语第三方库引入的样式
  "styles.css"
  ],
  "scripts": [ // 引入全局脚本,构建时会打包进来,常用语第三方库引入的脚本
  ],
  "environmentsource": "environments/environment.ts", // 基础环境配置
  "environments": { // 子环境配置文件
  "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": { // 执行`ng generate`命令时的一些默认值
 "styleext": "css", // 默认生成的样式文件后缀名
 "component": {
  "flat": false, // 生成组件时是否新建文件夹包装组件文件,默认为false(即新建文件夹)
  "spec": true, // 是否生成spec文件,默认为true
  "inlinestyle": false, // 新建时是否使用内联样式,默认为false
  "inlinetemplate": false, // 新建时是否使用内联模板,默认为false
  "viewencapsulation": "emulated", // 指定生成的组件的元数据viewencapsulation的默认值
  "changedetection": "onpush", // 指定生成的组件的元数据changedetection的默认值
 }
 }
}

为实现以.net core api项目为主体的站点结构,我们需在使用ng server时启用deploy选项,打开对静态资源“部署地址”的支持。注意:双站部署可能会产生js跨域,请自行解决

在命令行启动angular cli调试服务器时加上deploy参数 ng serve --deploy-url '//localhost:4200/' 

最后,通过vs的f5命令,打开api项目的运行时,我们可以看到网站的运行效果。enjoy coding~

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网