You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular Universal仅渲染router-outlet,路由内容不显示求助

问题:Angular Universal 路由内容无法渲染到router-outlet中

已尝试谷歌搜索相关问题,查阅过以下主题:

  • Angular Universal not rendering route
  • Angular Universal not rendering content inside of router-outlet
  • Angular universal rendering for some routes only

当前问题:router-outlet内的内容无法显示,服务端渲染后的HTML如下:

<app-root _nghost-sc17="" ng-version="14.2.0" ng-server-context="other">
<router-outlet _ngcontent-sc17=""></router-outlet><!----></app-root>

控制台及终端均无报错,相关配置代码如下:

server.ts

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

import {APP_BASE_HREF} from '@angular/common';
import {ngExpressEngine} from '@nguniversal/express-engine';
import * as express from 'express';
import {existsSync} from 'fs';
import {join} from 'path';

import {AppServerModule} from './src/main.server';

// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
  const server = express();
  const distFolder = join(process.cwd(), 'dist/food-recipe-front/browser');
  const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';

  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/main/modules/express-engine)
  server.engine('html', ngExpressEngine({
    bootstrap: AppServerModule,
  }));

  server.set('view engine', 'html');
  server.set('views', distFolder);

  // Example Express Rest API endpoints
  // server.get('/api/**', (req, res) => { });
  // Serve static files from /browser
  server.get('*.*', express.static(distFolder, {
    maxAge: '1y'
  }));

  // All regular routes use the Universal engine
  server.get('*', (req, res) => {
    res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
  });

  return server;
}

function run(): void {
  const port = process.env['PORT'] || 4000;

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

// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
  run();
}

export * from './src/main.server';

tsconfig.server.json

{
  "extends": "./tsconfig.app.json",
  "compilerOptions": {
    "outDir": "./out-tsc/server",
    "target": "es2019",
    "types": [
      "node"
    ]
  },
  "files": [
    "src/main.server.ts",
    "server.ts"
  ]
}

routes.ts

{path:'', component:HomeComponent, children: [
    { path:'home', component:HomeFrontComponent},
    { path: 'recipes', component:IndexRecipeComponent},
    { path: 'recipes/:strCategory', component:IndexRecipeComponent},
    { path: 'recipe-detail/:id', component:RecipeDetailComponent},
    { path: 'blogs', component:IndexBlogsComponent},
    { path: 'blog-detail/:id', component:BlogDetailComponent},
    { path: 'privacy', component:PrivacyComponent},
    { path: 'about-us', component:AboutUsComponent},
    { path: 'contact', component:ContactComponent},
  ]},

排查建议

  1. 补充默认子路由:当前根路由指向HomeComponent,但未设置默认子路由,访问根路径时无匹配的子组件渲染。可在子路由中添加{ path: '', redirectTo: 'home', pathMatch: 'full' },确保根路径能加载HomeFrontComponent。
  2. 验证AppServerModule配置:确认src/main.server.ts中导入的AppServerModule是否正确包含AppModule的路由配置,服务端模块未遗漏路由相关providers。
  3. 检查请求路径匹配:确保访问的URL(如/home)能被Express的*路由捕获并交给Angular Universal处理,未被静态文件路由拦截。
  4. 启用服务端日志:在server.ts中添加日志打印req.url,确认请求路径是否正确传递给Angular Universal。
  5. 检查组件服务端兼容性:HomeComponent或其子组件若使用了浏览器特有API(如window、document),需用isPlatformServer判断并避免在服务端执行相关代码。

内容的提问来源于stack exchange,提问作者Nani Po

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 18:11:02