将 Angular 4 Web 应用程序转换为 Angular Universal 应用程序 [英] Convert Angular 4 web app to Angular Universal app

查看:32
本文介绍了将 Angular 4 Web 应用程序转换为 Angular Universal 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 Angular 4 网络应用程序转换为 Angular Universal. 原因是目前,网络应用程序无法被谷歌正确索引(和 Facebook 社交预览缩略图),因为它是一个单页应用程序并在客户端呈现.所以现在我需要实现 Angular Universal,服务器侧面渲染.

I have a requirement to convert Angular 4 web app to Angular Universal.The reason for that is currently, the web app is not able to be properly indexed by Google (and Facebook social previews thumbnails) because it's a single page app and rendered on the client side.So now I need to implement Angular Universal, server-side rendering.

我了解了如何使用 这很棒视频系列.

I got the knowledge of how to create a new Angular Universal app using this great video series.

问题:您能否告诉我我必须遵循将现有应用程序转换为 Angular Universal 应用程序的方向.也许是一个不错的 URL 或步骤或任何方向将受到高度赞赏.

Question: Can you please tell me the direction which I have to follow the convert existing app as an Angular Universal one.Maybe a nice URL or steps or whatever the direction will be highly appreciated.

注意:这里的角度版本不是问题.24.

Note: Here angular version is not a problem.Either 2 or 4.

推荐答案

来自 此博客 (网络存档备份)

首先 npm install --save @angular/platform-server @angular/animations

编辑 /src/app/app.module.ts 并将 BrowserModule 导入更改为 BrowserModule.withServerTransition({appId: 'your-app-name'}),

Edit /src/app/app.module.ts and change the BrowserModule import to BrowserModule.withServerTransition({appId: 'your-app-name'}),

创建一个文件/src/app/app.server.module.ts

import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';

@NgModule({
imports: [
    ServerModule,
    AppModule
],
bootstrap: [AppComponent]
})
export class AppServerModule { }

创建文件/src/server.ts

import 'reflect-metadata';
import 'zone.js/dist/zone-node';
import { platformServer, renderModuleFactory } from '@angular/platform-server'
import { enableProdMode } from '@angular/core'
import { AppServerModuleNgFactory } from '../dist/ngfactory/src/app/app.server.module.ngfactory'
import * as express from 'express';
import { readFileSync } from 'fs';
import { join } from 'path';

const PORT = 4000;

enableProdMode();

const app = express();

let template = readFileSync(join(__dirname, '..', 'dist', 'index.html')).toString();

app.engine('html', (_, options, callback) => {
  const opts = { document: template, url: options.req.url };

  renderModuleFactory(AppServerModuleNgFactory, opts)
    .then(html => callback(null, html));
});

app.set('view engine', 'html');
app.set('views', 'src')

app.get('*.*', express.static(join(__dirname, '..', 'dist')));

app.get('*', (req, res) => {
  res.render('index', { req });
});

app.listen(PORT, () => {
  console.log(`listening on http://localhost:${PORT}!`);
});

编辑 /src/tsconfig.app.json 并将 server.ts 添加到 exclude 数组

Edit /src/tsconfig.app.json and add server.ts to the exclude array

编辑 /tsconfig.json 并在 "compilerOptions" 节点之后添加以下内容

Edit /tsconfig.json and after the "compilerOptions" node add the following

"angularCompilerOptions": {
    "genDir": "./dist/ngfactory",
    "entryModule": "./src/app/app.module#AppModule"
} 

编辑 package.json 并更改 "scripts" 部分,添加 "prestart" 并更改 "start"

Edit package.json and change the "scripts" section by adding "prestart" and altering "start"

"prestart": "ng build --prod && ngc",
"start": "ts-node src/server.ts"

您现在可以在控制台/终端窗口中键入 npm run start,它将构建所有内容并开始在端口 4000 上提供服务.

You can now type npm run start in your console/terminal window and it will build everything and start serving on port 4000.

它不会查看源代码或进行热模块替换等,但是一旦服务器运行,您还可以键入 ng serve(假设您使用的是 @angular/cli npm 包) 在另一个窗口中,像往常一样编辑单页应用程序,仅在重建或更改服务器代码时使用 npm run start.

It doesn't watch the source or do hot-module-replacing etc, but once the server is running you can also type ng serve (assuming you are using the @angular/cli npm package) in another window and edit your single-page-app as you normally would and use npm run start only when rebuilding or changing server code.

这篇关于将 Angular 4 Web 应用程序转换为 Angular Universal 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆