简体   繁体   English

将现有的Angular4 CLI迁移到.NET Core 2 Angular模板项目

[英]Migrating an existing Angular4 CLI to .NET Core 2 Angular template project

I have an existing Angular4 application built with the angular CLI. 我有一个使用angular CLI构建的现有Angular4应用程序。 Developed as normal, with index.html and main.ts as it's entry points. 正常开发,以index.htmlmain.ts为切入点。 The project users SASS for CSS. 项目用户使用SASS for CSS。

I now need to have this Angular app running inside a .NET Core web application (which I know was a headache in the past), so I know that in Visual Studio 2017, Microsoft had created new web templates especially for Angular and React. 我现在需要在.NET Core Web应用程序中运行此Angular应用程序(我知道这过去很头疼),所以我知道在Visual Studio 2017中,Microsoft创建了新的Web模板,尤其是Angular和React。

I've created a new project with the Angular template, copy & pasted the appropriate dependencies inside the packages.json file, copied the source from the src directory to the ClientApp directory, adjusted the default webapp file to load the appropriate files, but I get a lot of error messages (too many to even paste here). 我已经使用Angular模板创建了一个新项目,将相应的依赖项复制并粘贴到packages.json文件中,将源从src目录复制到ClientApp目录,调整了默认的Webapp文件以加载适当的文件,但是我收到很多错误消息(太多甚至无法粘贴到此处)。

I don't think I'm dealing with one specific problem, but with a whole different methodology for Angular development. 我认为我不是在处理一个特定的问题,而是在使用一种完全不同的Angular开发方法。 Is there some guide on how to convert an Angular4 CLI project to Visual Studio .NET Core 2 template? 是否有关于如何将Angular4 CLI项目转换为Visual Studio .NET Core 2模板的指南?

You can migrate an existing Angular-cli to to a .NET Core 2 Angular template project by the following steps. 您可以按照以下步骤将现有的Angular-cli迁移到.NET Core 2 Angular模板项目。

[ Note: You can download a repo which demonstrates this process here: https://github.com/johnpankowicz/angular-cli-to-SPA-template ] [注意:您可以在此处下载一个仓库来演示此过程: https : //github.com/johnpankowicz/angular-cli-to-SPA-template ]

Make sure node is v7.5.0 or above. 确保节点为v7.5.0或更高版本。

Create an Angular Asp.Net Core app using the SPA templates: 使用SPA模板创建Angular Asp.Net Core应用程序:

dotnet new --install Microsoft.AspNetCore.SpaTemplates::*
mkdir spa
cd spa
dotnet new angular
dotnet restore
npm install
start spa.csproj

Press F5 to test what you have so far. 按F5键测试您到目前为止所拥有的。

Remove sample components that the template generated: 删除模板生成的样本组件:

Delete HomeComponent, CounterComponent NavbarComponent FetchdataComponent and refs from app.module.shared.ts. 从app.module.shared.ts中删除HomeComponent,CounterComponent NavbarComponent FetchdataComponent和引用。

Edit app.component.html and replace contents with: 编辑app.component.html并将内容替换为:

    < h1 > Hello, world! < /h1 >

Press F5 to test your changes. 按F5测试您的更改。

Replace client app code with that of your own 用自己的代码替换客户端应用代码

Replace contents of ClientApp/app with the contents of your angular-cli project's "src/app" folder -- except leave the following files in ClientApp/app: 将ClientApp / app的内容替换为angular-cli项目的“ src / app”文件夹的内容-除了将以下文件保留在ClientApp / app中:

    app.module.browser.ts
    app.module.server.ts
    app.module.shared.ts

Edit app.module.browser.ts and app.module.server.ts. 编辑app.module.browser.ts和app.module.server.ts。 Fix the import references for app.component, if this file is not in the expected location. 如果此文件不在预期的位置,请修复app.component的导入引用。

Edit app.module.shared.ts. 编辑app.module.shared.ts。 Add the missing import statements and missing classes from the imports, exports and declarations required by your app. 从应用所需的导入,导出和声明中添加缺少的导入语句和缺少的类。 Note: do not include those that are already included in app.module.browser.ts or app.module.server.ts. 注意:不包括app.module.browser.ts或app.module.server.ts中已经包含的内容。

For now, to get your app running, you can add all your modules to app.module.shared.ts. 现在,要使您的应用程序运行,您可以将所有模块添加到app.module.shared.ts中。 But once you are familiar with server-side rendering, you can make use of running specific code only on the server or only in the browser. 但是一旦您熟悉了服务器端渲染,就可以利用仅在服务器上或仅在浏览器中运行特定的代码。

Edit Views/Home/Index.cshtml. 编辑Views / Home / Index.cshtml。 Change the app selector name to that defined in your AppComponent. 将应用选择器名称更改为AppComponent中定义的名称。 Add any additions that you made to your root index.html file. 将您添加的所有内容添加到根index.html文件中。

Edit configuration files 编辑配置文件

Edit the following files and change the AppComponent selector name from "app" to your selector name. 编辑以下文件,并将AppComponent选择器名称从“ app”更改为选择器名称。

    ClientApp/boot.browser.ts
    ClientApp/boot.server.ts 

Edit "package.json" and add any additional dependencies for your app that you had previously added to the angular-cli generated "package.json" file. 编辑“ package.json”,并为您先前添加到angular-cli生成的“ package.json”文件中的应用程序添加所有其他依赖项。

Edit tsconfig.json and add "jasmine" to the "types" array in "compileOptions". 编辑tsconfig.json并将“茉莉花”添加到“ compileOptions”中的“类型”数组中。 This adds types needed for angular-cli generated .spec files. 这将添加angular-cli生成的.spec文件所需的类型。

        "types": [
          "webpack-env",
          "jasmine" ]

Using Sass 使用Sass

In contrast to angular-cli, the AspNetCore.SpaTemplates place webpack.config.js in your project folder, where it can be modified. 与angular-cli相比,AspNetCore.SpaTemplates将webpack.config.js放在您的项目文件夹中,可以在其中进行修改。 In it you can enable support for Sass/Less. 您可以在其中启用对Sass / Less的支持。 Here is a guide for enabling Less support: https://github.com/aspnet/JavaScriptServices/tree/dev/src/Microsoft.AspNetCore.SpaServices#example-a-simple-webpack-setup-that-builds-less 这是启用更少支持的指南: https : //github.com/aspnet/JavaScriptServices/tree/dev/src/Microsoft.AspNetCore.SpaServices#example-a-simple-webpack-setup-that-builds-less

Note on debugging 调试注意事项

For debugging: Set the web browser to Google Chrome. 进行调试:将网络浏览器设置为Google Chrome。 There is currently an unresolved issue concerning breakpoints. 当前存在有关断点的未解决问题。 You need to set your breakpoints AFTER starting debugging (F5). 您需要在开始调试(F5)之后设置断点。 See: https://developercommunity.visualstudio.com/content/problem/125941/typescript-debugging-is-not-working-on-visual-stud.html As you can see from the comments, they consider this a lower priority bug to fix at this time. 请参阅: https : //developercommunity.visualstudio.com/content/problem/125941/typescript-debugging-is-not-working-on-visual-stud.html从评论中可以看到,他们认为这是优先级较低的错误在这个时候修复。 Hopefully it will move up in priority soon. 希望它将很快优先发展。

Another way to run angular-cli generated code in .Net Core 在.Net Core中运行angular-cli生成的代码的另一种方法

There is an easier approach to running an angular-cli app in .Net Core. 在.Net Core中有一种更简单的方法来运行angular-cli应用。 This is what I alluded to in my comment to the original question. 这是我在对原始问题的评论中提到的内容。 You can enclose the angular-cli app inside an empty Asp.Net Core web app. 您可以将angular-cli应用程序封装在一个空的Asp.Net Core Web应用程序内。 To see how, go to: https://stackoverflow.com/a/47229442/1978840 要查看操作方法,请访问: https : //stackoverflow.com/a/47229442/1978840

In my opinion it still being a really hard work. 我认为这仍然是一项艰巨的工作。 What we did in our project was to start a new .NET core angular project from the scratch using Visual Studio 2017 and started putting file by file. 我们在项目中所做的就是使用Visual Studio 2017从头开始一个新的.NET核心角度项目,并开始逐个文件放置文件。 Start with one of your minimalist component. 从您的极简主义组件开始。 Put all the scripts and theme, amend the routes, and other suggestion is to upgrade some of things like npm, typescript (take care because some problems are regarding incompatibility of version). 放置所有脚本和主题,修改路径,其他建议是升级某些内容,例如npm,打字稿(请注意,因为某些问题与版本的不兼容性有关)。 However, after some work you still with some issues then make some search or put an specific error to the community. 但是,经过一些工作之后,您仍然会遇到一些问题,然后进行一些搜索或向社区提出特定的错误。

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

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