我正在尝试在Nodejs + Expressjs上使用Angular Universal渲染Angular应用程序。通过ng build --prod --app 1
构建应用程序并使用webpack编译后,通过node server.js
运行应用程序。当我在浏览器中打开应用程序时,会出现错误当前文档没有doctype。这可能会导致某些Angular Material组件的行为不如预期。
// Load zone.js for the server.
require('zone.js/dist/zone-node');
require('reflect-metadata');
import * as express from 'express';
import { enableProdMode, ValueProvider } from '@angular/core';
import { renderModuleFactory } from '@angular/platform-server';
import { join } from 'path';
import { readFileSync } from 'fs';
const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist-
server/main.bundle');
const template = readFileSync('./src/index.html', { encoding: 'utf8' });
const app = express();
app.engine('html', (_, options, callback) => {
const opts = {
document: template,
url: options.req.url,
extraProviders: [
<ValueProvider>{
provide: 'REQUEST',
useValue: options.req,
},
provideModuleMap(LAZY_MODULE_MAP)
]
};
renderModuleFactory(AppServerModuleNgFactory, opts)
.then(html => callback(null, html));
});
app.set('view engine', 'html');
app.set('views', 'src');
app.get('*.*', express.static(join(__dirname, 'dist-server')));
app.get('*', (req, res) => {
res.render('index', {req});
});
app.listen(5000, () => {
console.log('listening on http://localhost:5000!');
});
看起来这个bug与@angular/material有关,将来会得到修复,但是否有任何解决办法?