Angular Material + Angular Universal

I'm trying to render Angular app with Angular Universal on Nodejs + Expressjs on server side. After building app via ng build --prod --app 1 and compiling with webpack and run app via node server.js . When I open app in browser get error Current document does not have a doctype. This may cause some Angular Material components not to behave as expected. Current document does not have a doctype. This may cause some Angular Material components not to behave as expected.

// 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!');
});

Looks like this bug related to @angular/material and will be fixed in the future, but is there any workaround?


Currently Angular Material 2 are not supported by Angular Universal. https://github.com/angular/universal#in-progress

链接地址: http://www.djcxy.com/p/31366.html

上一篇: 客户端异步。 请求与服务器端渲染

下一篇: 角度材料+角度通用