Angular6 通用捐赠继续在控制台上出错

Angular6 universal giving continues Error on console

我正在使用 angular 6 并为服务器端页面呈现实现了 angular 通用, 一切正常,但我的控制台继续出现错误。

ERROR [Error]
ERROR [Error]

而且我已经将 window 声明为全局变量,但我仍然收到这样的错误

Unhandled Promise rejection: window.some_function is not a function

我的 server.ts 文件 //

import 'zone.js/dist/zone-node';
import 'reflect-metadata';

import { enableProdMode } from '@angular/core';

import * as express from 'express';
import { join } from 'path';
// Express Engine
import { ngExpressEngine } from '@nguniversal/express-engine';
// Import module map for lazy loading
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';

// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();

// node run port
const port = 6065;

// Express server
const app = express();

const PORT = port || process.env.PORT;
const DIST_FOLDER = join(process.cwd(), 'dist');

const domino = require('domino');
const fs = require('fs');
const MockBrowser = require('mock-browser').mocks.MockBrowser;
const mock = new MockBrowser();

const template = fs
    .readFileSync(join(DIST_FOLDER, 'browser', 'index.html'))
    .toString();
// Make all Domino types available as types in the global env.
Object.assign(global, domino.impl);
(global as any)['KeyboardEvent'] = domino.impl.Event;
const win = domino.createWindow(template);
win.Object = Object;
win.Math = Math;

global['window'] = win;
global['document'] = win.document;
global['navigator'] = mock.getNavigator();
global['branch'] = null;
global['object'] = win.object;
global['HTMLElement'] = win.HTMLElement;
global['DOMTokenList'] = win.DOMTokenList;
global['Node'] = win.Node;
global['Text'] = win.Text;

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');

app.engine('html', ngExpressEngine({
    bootstrap: AppServerModuleNgFactory,
    providers: [
        provideModuleMap(LAZY_MODULE_MAP)
    ]
}));

app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));

// TODO: implement data requests securely
app.get('/api/*', (req, res) => {
    res.status(404).send('data requests are not supported');
});

// Server static files from /browser
app.get('*.*', express.static(join(DIST_FOLDER, 'browser')));

// All regular routes use the Universal engine
app.get('*', (req, res) => {
    res.render('index', { req });
});

// Start up the Node server
app.listen(PORT, '0.0.0.0', () => {
    console.log(`Node server listening on http://localhost:${PORT}`);
});

我遇到了同样的问题并开始在我的 Visual Studio 代码中调试 server.js 以及我发现的内容:错误消息是由来自 TranslateHttpLoader 的 http 请求引起的(我是使用 ngx-translate),它试图加载 .json-带有翻译的文件。经过一番谷歌搜索后,我发现了这个 thread,您可以在其中找到如何启用服务器端翻译加载。错误消息消失后​​。