简体   繁体   English

角度材料+角度通用

[英]Angular Material + Angular Universal

I'm trying to render Angular app with Angular Universal on Nodejs + Expressjs on server side. 我正试图在服务器端的Nodejs + Expressjs上使用Angular Universal渲染Angular应用程序。 After building app via ng build --prod --app 1 and compiling with webpack and run app via node server.js . 通过ng build --prod --app 1并使用webpack进行编译并通过node server.js运行app。 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. 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? 看起来这个与@ angular / material相关的bug将来会修复,但是有没有解决办法?

Currently Angular Material 2 are not supported by Angular Universal. 目前Angular Universal不支持Angular Material 2。 https://github.com/angular/universal#in-progress https://github.com/angular/universal#in-progress

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

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