Angular Universal无法在路由器出口内呈现内容

时间:2019-05-29 21:10:46

标签: angular angular7 angular-universal universal ssr

我正在尝试通过此链接https://angular.io/guide/universal使用Angular Universal使用SSR渲染Angular 7应用程序。我以为我可以从那里开始一切工作,因为我能够查看页面源代码并看到呈现给页面的app.component内部的所有内容。但是,我无法在router-outlet标签内显示任何路由

我只是简单地剥离了我的应用程序,以完全按照文档中的说明进行操作。我在其中读到了一些有关确保ModuleMapLoaderModule位于app.server.module内的内容,以便可以进行延迟加载。默认情况下,angular-cli命令会附带此命令,因此我不确定发生了什么。

app.server.module

import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { ModuleMapLoaderModule } from '@nguniversal/module-map-ngfactory-loader';

import { AppComponent } from './app.component';
import { AppModule } from './app.module';

@NgModule({
  imports: [
    AppModule,
    ServerModule,
    ModuleMapLoaderModule,
  ],
  bootstrap: [AppComponent],
})
export class AppServerModule {}

tsconfig.server.json

  "extends": "./tsconfig.app.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app-server",
  },
  "angularCompilerOptions": {
    "entryModule": "app/app.server.module#AppServerModule"
  }
}

server.ts

import 'zone.js/dist/zone-node';
import { enableProdMode } from '@angular/core';
// Express Engine
import { ngExpressEngine } from '@nguniversal/express-engine';
// Import module map for lazy loading
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';

import * as express from 'express';
import { join } from 'path';

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

// Express server
const app = express();

const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'browser');

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

// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(LAZY_MODULE_MAP)
  ]
}));

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

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

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

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

渲染输出

<html lang="en">

<head>
    <base href="/">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- ...Rendered CSS styles... -->
<body>
    <app-root _nghost-sc0="" ng-version="7.2.15">
        <!-- ...Header code that has been rendered... -->

        <router-outlet _ngcontent-sc0=""></router-outlet> <!-- This is where I am expecting the content of the route to be -->

        <!-- ...Footer code that has been rendered... -->
    </app-root>
</body>

</html>

app.routing.module.ts

// Angular Imports

// Pages
// ...Page Imports..

const routes: Routes = [
  { path: '', component: HomePageComponent, pathMatch: 'full' },
  { path: 'events/:id', component: EventDetailPageComponent }, // This is the detail page I am testing with
  { path: 'unknown-error', component: UnknownErrorPageComponent },
  { path: '404', component: NotFoundPageComponent },
  { path: '**', redirectTo: '404' }
];

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forRoot(routes),
  ],
  exports: [
    RouterModule,
  ]
})
export class AppRoutingModule { }

我一直很幸运,试图找到有关Angular 7的准确文档,任何帮助将不胜感激!谢谢

1 个答案:

答案 0 :(得分:0)

我对服务器端渲染没有任何经验,但是将Angular应用程序部署到服务器时,常见的问题是子目录的解析。基本上,根将加载,但通过url访问时,在角路由中处理的所有其他项目将不起作用。 Web服务器正在寻找这些物理目录,而不是允许Angular路由将页面定向到虚拟目录。如果这听起来像是您的问题,那么可以选择here