我们有一个正在迁移的Angular6和AngularJS 1.6混合项目。在此过程中,我们还想介绍服务器端渲染(SSR)。
问题是,SSR在节点环境中运行,因此运行SSR / prerendering会出现以下错误:
npm-run ts-node ./prerender.ts [..]/node_modules/angular/index.js:2 **module.exports = angular;** **ReferenceError: angular is not defined** at Object.<anonymous> ([..]/node_modules/angular/index.js:2:18) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object.angular ([..]/dist/server/webpack:/external "angular":1:1) at _webpack_require_ ([..]/dist/server/webpack:/webpack/bootstrap:19:1)
import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import {renderModuleFactory} from '@angular/platform-server';
import {writeFileSync} from 'fs';
const globalAny:any = global;
const Window = require('window');
let window = new Window();
globalAny.window = window;
globalAny.document = window.document;
const {AppServerModuleNgFactory} = require('./dist/server/main');
renderModuleFactory(AppServerModuleNgFactory, {
document: '<app-root></app-root>',
url: '/'
})
.then(html => {
console.log('Pre-rendering successful, saving prerender.html');
writeFileSync('./prerender.html', html);
})
.catch(error => {
console.error('Error occurred:', error);
});
该项目基于通用起动器存储库:https://github.com/angular/universal-starter
有什么建议吗?