简体   繁体   English

MVC项目的Angular2设置

[英]Angular2 setup for MVC project

I'm trying to figure out the best ways & practices for integrating Angular2 into a legacy Spring MVC framework. 我正在尝试找出将Angular2集成到旧版Spring MVC框架中的最佳方法和做法。 This is not specific to Spring MVC but to any MVC framework. 这不是特定于Spring MVC,而是特定于任何MVC框架。

In Angular1 world , the server rendered the page and Angular1 could be used purely for client side functionality. 在Angular1世界中,服务器渲染了页面,而Angular1可以纯粹用于客户端功能。 There would be 1 ng-app per page. 每页会有1 ng-app。

From what I've seen from the use of Angular (Typescript + Angular-CLI) looks like its ideally suited for a Single Page App, where the html is rendered by a node webserver. 从我对Angular(Typescript + Angular-CLI)的使用中所见,它看起来非常适合于单页面应用程序,其中html是由节点Web服务器呈现的。

I've also looked into Angular2 (using plain js), but seems like some of the functionality, available in Typescript is missing. 我也研究了Angular2(使用纯js),但似乎缺少Typescript中可用的某些功能。 For example, templateUrl property. 例如, templateUrl属性。

To summarize what is the recommended setup to integrate Angular2 into an MVC framework? 总结一下将Angular2集成到MVC框架中的推荐设置是什么?

Requirements 要求

In local dev env, I want to be able to have similar functionality which ng serve provides (ie live reload) but the page itself will be rendered by the server 在本地开发环境中,我希望能够具有ng serve提供的类似功能(即实时重新加载),但是页面本身将由服务器呈现

In prod, I should be able to bundle up and do the production build best practices. 在生产中,我应该能够捆绑并进行生产构建最佳实践。

Thank you in advance. 先感谢您。

Before going all in with Angular 2 for the use case that you have, it may be a good idea to survey the many options out there, even if you are coming from a Angular 1.x background. 在继续使用Angular 2进行用例开发之前,最好调查一下其中的许多选项,即使您来自Angular 1.x背景也是如此。 The reason I'm saying this is because I just recently used Angular with another MVC server side framework on a project. 我之所以这样说是因为我最近在项目上将Angular与另一个MVC服务器端框架一起使用。 Here are a few shortcomings/headaches I experienced. 这是我遇到的一些缺点/头痛。

Server Rendering 服务器渲染

One of the things that was convenient when using Angular 1.x was the ability to render templates on the server like so. 使用Angular 1.x时最方便的事情之一就是能够像这样在服务器上呈现模板。

<div ng-controller="HomeController as vm" >
      <!-----Home.Html------>

      <!------Server Logic/Bindings----->

 </div>

Just quickly data bind and render your home template on the server, and then home.html comes to life after the Angular compilation process. 只需快速地将数据绑定并在服务器上呈现您的主页模板,然后Angular编译过程就会使home.html栩栩如生。 It seems like pulling templates from the server is not supported or recommended in Angular. 似乎在Angular中不支持也不建议从服务器中提取模板。 This is a big draw back with Angular 2 in my opinion. 我认为,这与Angular 2相比是一个很大的缺点。

Now I believe server rendering can be achieved with Angular 2, but I think it can only occur during the initial page load using the Angular Universal module. 现在,我相信可以使用Angular 2来实现服务器渲染,但是我认为只能在使用Angular Universal模块进行初始页面加载期间进行。

Tooling 工装

There is so much tooling that is required if you want to author an Angular 2 app. 如果要编写Angular 2应用程序,则需要太多工具。 Typescript, or ES6, and in most cases your going to need a separate text editor like VS Code or Webstorm. Typescript或ES6,在大多数情况下,您将需要单独的文本编辑器,例如VS Code或Webstorm。 My enterprise edition of Visual Studio 2015 doesn't even cut if for Angular development anymore. 我的企业版Visual Studio 2015甚至不再用于Angular开发。 So I do all my Angular development with Webstorm in complete isolation from my server side framework. 因此,我使用Webstorm进行所有的Angular开发,并且与服务器端框架完全隔离。

One of the most frustrating things with many of the newer JS frameworks is how you deliver the App to the browser. 许多更新的JS框架中最令人沮丧的事情之一就是如何将应用程序交付到浏览器。 This is where a build system comes in. This aspect of the development process is probably the most complicated. 这就是构建系统的用武之地。开发过程的这一方面可能是最复杂的。 So your pretty much forced to use a seed project with an already well tuned build process. 因此,您几乎被迫使用种子项目,并且该种子项目的构建过程已经很好地进行了调整。 Angular-cli seems like the best bet currently. Angular-cli似乎是目前最好的选择。 Even with these great starter projects, I had to create an additional gulp process to automate a few things. 即使有了这些出色的入门项目,我也不得不创建一个额外的gulp流程来自动化一些事情。

With these things in mind you may want to see what other options are available that may be better compatible to a more traditional MVC server approach. 考虑到这些因素,您可能想看看还有哪些其他选项可以更好地与更传统的MVC服务器方法兼容。 I've heard good things about VueJs . 我听说过有关VueJs的消息 React may even be better suited for such an approach. React甚至可能更适合这种方法。 I'm strongly considering both frameworks, or perhaps even VanillaJS for smaller scenarios. 我强烈考虑两个框架,甚至对于较小的场景,甚至考虑使用VanillaJS。

This is not to say that Angular is not an awesome framework. 这并不是说Angular不是一个很棒的框架。 I just think the tooling is not quite there yet, and it may not be the best tool for every scenario. 我只是认为该工具还不存在,并且可能不是每种情况下的最佳工具。 With that in mind this is the approach I took using the Angular-Cli. 考虑到这一点,这就是我使用Angular-Cli采取的方法。

Angular-Cli.json 角Cli.json

When the Angular-cli produces a build. 当Angular-cli生成构建时。 Your going to likely want that build to output to the public directory of your server-side framework. 您可能希望将该构建输出到服务器端框架的公共目录。 This can be accomplished by modifying the outDir property of the angular-cli.json file. 这可以通过修改angular-cli.json文件的outDir属性来完成。

"outDir": "../MVC-Project/wwwroot"

Proxy to Server API 服务器API的代理

When using ng serve, you will likely want to proxy all your api calls to your server project. 使用ng serve时,您可能希望将所有api调用代理到服务器项目。 This can be accomplished by adding a proxy.conf.json file to the root of you CLI project: 这可以通过在CLI项目的根目录中添加proxy.conf.json文件来完成:

   {
      "/api": {
        "target": "http://localhost:8000",
        "secure": false
      }
    }

Serving the App 服务应用

Then you'll need to include your "APP" component somewhere within your server rendered templates. 然后,您需要在服务器呈现的模板中的某个位置包含“ APP”组件。 For simplicity sake, let's say this is your index.html file. 为了简单起见,假设这是您的index.html文件。 The tags in my example below are .Net Core related, but hopefully you get the idea. 下面示例中的标签与.Net Core相关,但希望您能理解。

<app-root>
    <div class="loader"></div>
</app-root>
  @section scripts{
        <environment names="Development, Staging">
            <script type="text/javascript" src="inline.bundle.js"></script>
            <script type="text/javascript" src="scripts.bundle.js"></script>
            <script type="text/javascript" src="styles.bundle.js"></script>
            <script type="text/javascript" src="vendor.bundle.js"></script>
            <script type="text/javascript" src="main.bundle.js"></script>
        </environment>
        <environment names="Production">

            <!-- inject:js -->

            <!-- endinject -->
        </environment>
       }

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

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