更新: SpaTemplates 已更新到 Angular 4,这意味着您应该能够获得 SSR 以及其他好处,而无需删除任何内容。 https://www.nuget.org/packages/Microsoft.AspNetCore.SpaTemplates
原创:听起来您正在寻找的模板可能并不完全存在于野外。如果您想要 .NET Core、Angular 4、HMR 和 Webpack,您可以从当前使用 Angular 2 的 Angular SPA 模板开始。
在命令行运行以下命令。
dotnet new --install Microsoft.AspNetCore.SpaTemplates::*
dotnet new angular
(取自https://jonhilton.net/2017/02/21/create-a-vs2017-angular-2-and-net-core-site-using-the-command-line/)
然后您可以使用以下更改来修改项目以删除 Angular 2 通用和服务器端渲染,以便能够升级到 Angular 4。
webpack.config.vendor.js
从供应商部分删除 angular2-universal
从供应商部分删除 angular2-universal-polyfills
删除 serverBundleConfig
替换:
new webpack.ContextReplacementPlugin(/\@angular\b.*\b(bundles|linker)/, path.join(__dirname, './ClientApp')),
与:
new webpack.ContextReplacementPlugin(/angular(\\|\/)core(\\|\/)@angular/, path.join(__dirname, './ClientApp')),
webpack.config.js
删除 serverBundleConfig
package.json
添加了角度/动画、core-js、es6-promise
将 Angular 包升级到 ^4.0.0
删除了 angular2-universal、angular2-universal-patch、angular2-universal-polyfills、isomorphic-fetch
将其他包升级到当时的最新版本
Views/Home/Index.cshtml
删除了 asp-prerender-module="ClientApp/dist/main-server"
ClientApp/polyfills/polyfills.ts
添加了以下文件:
/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/set';
/** Evergreen browsers require these. **/
import 'core-js/es6/reflect';
import 'core-js/es7/reflect';
import 'zone.js';
ClientApp/boot-server.ts
删除了这个文件
ClientApp/boot-client.ts
将文件更改为以下内容:
import './polyfills/polyfills.ts';
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
const rootElemTagName = 'app'; // Update this if you change your root component selector
// Enable either Hot Module Reloading or production mode
if (module['hot']) {
module['hot'].accept();
module['hot'].dispose(() => {
// Before restarting the app, we create a new root element and dispose the old one
const oldRootElem = document.querySelector(rootElemTagName);
const newRootElem = document.createElement(rootElemTagName);
oldRootElem.parentNode.insertBefore(newRootElem, oldRootElem);
platform.destroy();
});
} else {
enableProdMode();
}
// Boot the application, either now or when the DOM content is loaded
const platform = platformBrowserDynamic();
const bootApplication = () => { platform.bootstrapModule(AppModule); };
if (document.readyState === 'complete') {
bootApplication();
} else {
document.addEventListener('DOMContentLoaded', bootApplication);
}
ClientApp/app/app.module.ts
将顶部导入中的 UniversalModule 替换为:
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
将导入区域中的 UniversalModule 替换为:
BrowserModule,
HttpModule,
当这一切都完成后,我运行以下命令:
npm prune
npm install
webpack --config webpack.config.vendor.js
(取自https://github.com/aspnet/JavaScriptServices/issues/938)
我已经验证这将使 Angular 4 在 .NET 核心上运行,并且仍然允许 HMR 和 Webpack 工作。