未在服务器端呈现的页面-<app-root> </app-root>`仍未编译

时间:2019-09-17 15:48:49

标签: angular angular-universal

设置通用配置后,修复所有与DOM / BOM对象有关的编译错误,页面的服务器端渲染无法正常工作

package.json

"@angular/core": "^7.2.9",
"@angular/platform-browser": "^7.2.9",
"@angular/platform-browser-dynamic": "^7.2.9",
"@angular/platform-server": "^7.2.8",
"@nguniversal/common": "^8.1.1",
"@nguniversal/express-engine": "^7.1.1",
"@nguniversal/module-map-ngfactory-loader": "7.1.1",

app.module.ts

imports : [
    BrowserModule.withServerTransition({ appId: 'serverApp' }),
    BrowserTransferStateModule,
    ...
  ]

server.ts

...
app.engine('html', (_, options, callback) => {
  const engine = ngExpressEngine({
    bootstrap: AppServerModuleNgFactory,
    providers: [
      provideModuleMap(LAZY_MODULE_MAP),
      {provide: 'host', useFactory: () => options.req.get('host'), deps: []}
    ]
  });
  engine(_, options, callback);
});

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

// Serve static files from /browser
app.get('*.*', express.static(join(DIST_FOLDER, 'browser'), {
  maxAge: '1y'
}));

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

app.get('*', (req, res) => { res.sendFile( join(DIST_FOLDER, 'browser' , 'index.html'), {req}); });
...

我的项目出了什么问题?

0 个答案:

没有答案