简体   繁体   English

如何正确设置一个新的Angular 6项目

[英]How to properly set up a new Angular 6 project

On my way to learning Angular, I have come across two different ways of creating a new Angular project. 在我学习Angular的过程中,我遇到了两种创建新Angular项目的方法。

The first is using the Angular CLI command: 第一种是使用Angular CLI命令:

ng new app-name-here

The second is using the command: 第二个是使用命令:

dotnet new angular app-name-here

The generated projects seem very different, and there are lots of parts for me to cover. 生成的项目看起来非常不同,我需要介绍很多部分。 What's the difference between the two approaches? 这两种方法有什么区别? What are the pros and cons for each approach when the goal is to build a client app that will communicate with a given ASP.Net Core API? 当目标是构建与给定ASP.Net Core API通信的客户端应用程序时,每种方法的优缺点是什么?

The template used by dotnet new angular comes from Microsoft . dotnet new angular使用的模板来自微软

It's a frozen in time template, which has the obvious disadvantage that it is always out of date since Angular is changing ALL THE TIME. 这是一个冻结的时间模板,其明显的缺点是它总是过时,因为Angular一直在变化。

Often you will be able to just say ng update and/or follow the Angular Update Guide to get the latest updates. 通常,您只需说ng update和/或按照Angular更新指南获取最新更新即可。 However after major releases this is a bigger problem - right now the template uses Angular 5 and Angular 6 is the latest. 然而,在主要版本发布之后,这是一个更大的问题 - 现在模板使用Angular 5,而Angular 6是最新的。

The template has some advantages and disadvantages: 该模板有一些优点和缺点:

Advantages 好处

  • You know it will work out of the box, and it's a good start for a 'playground' project. 你知道它可以开箱即用,对于一个“游乐场”项目来说这是一个很好的开始。
  • It includes bootstrap and a more usable sample than the angular ng new . 它包括bootstrap和比angular ng new更有用的样本。
  • It includes WebAPI sample code too (which is distinct from Angular code) so you can quickly add new API controllers. 它还包括WebAPI示例代码(与Angular代码不同),因此您可以快速添加新的API控制器。

Disadvantages 缺点

  • Sometimes Angular makes MAJOR changes between updates. 有时Angular会在更新之间进行MAJOR更改。 Version 5-6 is such an example where .angular-cli.json is now angular.json and completely different format. 版本5-6就是这样一个例子,其中.angular-cli.json现在是angular.json ,格式完全不同。 If you're new to angular this just increases the overall headache. 如果你是刚接触角度,这只会增加整体头痛。
  • ng update never seems to work for me very reliably. ng update似乎永远不会对我有效。 Check out the Angular upgrade guide too. 查看Angular升级指南。
  • You may not want bootstrap css included. 您可能不希望包含bootstrap css。
  • If you've already got an Angular project you want to dotnet-ify it doesn't do that for you easily by itself. 如果你已经有了一个Angular项目,你想要dotnet-ify它不会轻易地为你做到这一点。

Because of the major changes from 5>6 I recommend not using this template right now except for casual testing. 由于5> 6的主要变化,我建议不要立即使用此模板,除非进行临时测试。 .

For me I found the best thing to do is to follow these steps: 对我来说,我发现最好的办法是遵循以下步骤:

  • Run dotnet new angular or dotnet new angular -o projectname to put it in a subdirectory. 运行dotnet new angulardotnet new angular -o projectname将其放在子目录中。 This gets your Spa template setup to connect dotnet world to angular world. 这将获得您的Spa模板设置,以将dotnet世界连接到角度世界。
  • Wipe out completely the contents of the ClientApp folder it created for you - or rename it so you can refer to the sample code if you're new to Angular 完全擦除它为您创建的ClientApp文件夹的内容 - 或重命名它,以便您可以参考示例代码,如果您是Angular的新手
  • CD to the ClientApp folder CD到ClientApp文件夹
  • Use the Angular CLI (separate install from angular team) and run ng new --help to see all available options 使用Angular CLI (从angular团队单独安装)并运行ng new --help以查看所有可用选项
  • Create your angular app. 创建你的角度应用。 These are the options I use: 这些是我使用的选项:

ng new sjw_website --routing --style=scss -prefix=sjw

The prefix is what your custom component elements begin with (eg. The 'name' is the folder created (which will be inside ClientApp) 前缀是您的自定义组件元素的开头(例如,'name'是创建的文件夹(将在ClientApp中)

  • Run your Angular app using npm start which fires it up on port 4200 (run this from the new folder in ClientApp/sjw_website ). 使用npm start运行你的Angular应用npm start ,它在端口4200上npm start它(从ClientApp/sjw_website的新文件夹运行它)。 You can view this immediately at http://localhost:4200 which is completely separate from dotnet and running in Angular's own server. 您可以立即在http:// localhost:4200上查看此内容,该内容与dotnet完全分开并在Angular自己的服务器中运行。
  • Modify the SPA template in startup.cs to use this line of code spa.UseProxyToSpaDevelopmentServer("http://localhost:4200"); 修改startup.csSPA模板以使用这行代码spa.UseProxyToSpaDevelopmentServer("http://localhost:4200"); This will proxy requests to the angular server. 这将代理对角度服务器的请求。
  • Modify the startup.cs to point to ClientApp/sjw_website (or move the files manually up a level) 修改startup.cs以指向ClientApp/sjw_website (或手动将文件移动到某个级别)
  • Open a second powershell to build and run (in the folder containing your dotnet project) 打开第二个powershell来构建和运行(在包含你的dotnet项目的文件夹中)
  • Build dotnet build and run dotnet run 构建dotnet build并运行dotnet run
  • Alternatively use dotnet watch run 或者使用dotnet watch run

Note that the angular project which is running under the angular ng serve development server will automatically restart when you make changes to the angular files. 请注意,在更改角度文件时,角度ng serve开发服务器下运行的角度项目将自动重新启动。 When using dotnet watch the dotnet server will restart when you make changes to dotnet files. 使用dotnet watch时,当您对dotnet文件进行更改时,dotnet服务器将重新启动。

Having both open will be your best experience, preferably on a second monitor. 打开两个将是您的最佳体验,最好是在第二台显示器上。

Note that when you do a production build it is the npm build command embedded in the csproj file that does the magic and points the output to a dist folder. 请注意,当您执行生产构建时,嵌入在csproj文件中的npm build命令会执行魔术并将输出指向dist文件夹。

ng new 新的

Creates a new angular project using the CLI. 使用CLI创建新的角度项目。 It won't create any of the backend code/projects you need 它不会创建您需要的任何后端代码/项目

dotnet new angular dotnet新角度

Creates a new .NET core project based on a MS provided template. 基于MS提供的模板创建新的.NET核心项目。 It also creates (most?) of the files you will need for the Angular portion. 它还会创建(大多数?)Angular部分所需的文件。 Since you want an ASP.NET Core backend I would go this route if I wanted a one-command solution. 既然你想要一个ASP.NET核心后端,我会想要一个单命令解决方案。 I haven't used it myself so your mileage my vary. 我自己没有用过,所以你的里程我的变化。

Manual 手册

You can always create an ASP.NET Core WebAPI project in Visual Studio and then ng new an application inside it. 您始终可以在Visual Studio中创建ASP.NET Core WebAPI项目,然后其中ng new一个应用程序。 This is the route I typically take. 这是我通常采取的路线。

Edit (31/MAY/2018) 编辑(201年5月31日)

Today I created a DotNet Project Template that should facilitate these steps for anyone. 今天我创建了一个DotNet项目模板,可以为任何人提供这些步骤。 You could try it here: 你可以在这里试试:

AspNetCore2Ang6Template: https://github.com/JuanGarciaCarmona/AspNetCore2Ang6Template AspNetCore2Ang6Template: https //github.com/JuanGarciaCarmona/AspNetCore2Ang6Template

END Edit (31/MAY/2018) 结束编辑(201 / MAY / 2018)

To the second question: 对第二个问题:

What are the pros and cons for each approach when the goal is to build a client app that will communicate with a given ASP.Net Core API? 当目标是构建与给定ASP.Net Core API通信的客户端应用程序时,每种方法的优缺点是什么?

IMHO best choice is to have both in same project because you can deploy and serve the client side (angular) and the API (ASP NET Core Web API controllers) from a single project, something that is good in dev stage and facilitates deployment. 恕我直言最好的选择是同时拥有两个项目,因为您可以从单个项目部署和服务客户​​端(角度)和API(ASP NET核心Web API控制器),这在开发阶段是很好的并且便于部署。 To acomplish so best choice is to mix both mechanisms, you can create the dotnet project and inside it the place the angular code also insed it, add and use MVC to you app and serve the angular app. 为了实现这么好的选择是混合两种机制,你可以创建dotnet项目,在其里面角度代码也包含它的地方,添加和使用MVC到你的应用程序并提供角度应用程序。

Easiest recipe to do so is to: 最简单的配方是:

  1. Create a dotnet project (Empty ASP NET core Web Application) and name it XXXXXX, that can be done from Visual Studio or form command line ('dotnet new web') 创建一个dotnet项目(空ASP NET核心Web应用程序)并将其命名为XXXXXX,可以从Visual Studio或表单命令行('dotnet new web')完成
  2. Then, from command line go to the prevous folder and type ng new XXXXXX, that command would place everything you need inside the previously created dotnet project folder. 然后,从命令行转到prevous文件夹并键入ng new XXXXXX,该命令会将您需要的所有内容放在先前创建的dotnet项目文件夹中。
  3. Once it finish go to angular.cli and set the output to be 'wwwroot' (instead of 'dist/XXXXXX' 4.- Your startup class should be like: 一旦完成,请转到angular.cli并将输出设置为'wwwroot'(而不是'dist / XXXXXX'4 .-您的启动类应该是:

     public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); app.UseDefaultFiles(); app.UseStaticFiles(); 

    } } }}

And then you can run it using dotnet run or ng serve, both commands work. 然后你可以使用dotnet run或ng serve来运行它,这两个命令都可以运行。

One thing that, at least for me is missing, is that live reloading is not working, as soon as I fix it for me I'll update this answer. 有一件事,至少对我来说是缺失的,是实时重新加载不起作用,一旦我为我修复它我会更新这个答案。

I hope it helps, 我希望它有所帮助,

Juan 胡安

Edit (26/MAY/2018) 编辑(26 / MAY / 2018)

Regarding live reload it has been impossible to me achieve that goal by debugging from VS or running the app with dotnet run but to check styles and small but tricky customisation what I've done was to launch the web app with 'ng serve'. 关于实时重新加载,我无法通过从VS调试或使用dotnet运行运行应用程序来实现这一目标,但是检查样式和小而棘手的自定义我所做的是使用'ng serve'启动Web应用程序。

If you need both, client and server up and running, because you need to call your api controllers from your client app, then you might play with environment.ts configuring your api base url and during that time run your backend from VS and your front end with ng serve. 如果您需要同时启动和运行客户端和服务器,因为您需要从客户端应用程序调用api控制器,那么您可以使用environment.ts配置您的api基本URL,并在此期间从VS和您的前端运行您的后端用ng服务结束。 hopefully this will be solved or fixed in future VS versions or, as I've seen was requested, in newer asp net core SPA templates prepared for Angular 6 希望这将在未来的VS版本中得到解决或修复,或者正如我所见,在为Angular 6准备的更新的asp net core SPA模板中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM