仅某些路线的角度通用渲染

And*_*vic 6 javascript angular-universal angular

我正在使用角度通用,但找不到用于仅对某些页面(如主页)使用服务器端渲染并以标准角度方式渲染所有其他路线的选项。我不想在不需要SEO的私有页面上使用服务器端渲染。我可以这样配置快速路由

// send all requests to Angular Universal
// if you want Express to handle certain routes (ex. for an API) make sure you adjust this
app.get('/', ngApp);
app.get('/home', ngApp);
app.get('/about', ngApp);
Run Code Online (Sandbox Code Playgroud)

理想情况下,我完全不需要了解NodeJ,并使用有如serverSide这样的属性在有角路由配置中对其进行配置:true

const appRoutes: Routes = [
  //public route, I want server rendering for SEO
  { path: 'home', component: HomeComponent, serverSide: true },
  //private user profile page, SEO is not needed
  { path: 'user/profile/:id', component: UserProfileComponent },
];
Run Code Online (Sandbox Code Playgroud)

小智 8

在你不想渲染的路由的 server.ts 中,请执行以下操作

app.get('/api/**', (req, res) => { });

app.get('/app/**', (req, res) => {
  console.log('not rendering  app pages');
});

app.get('/auth/**', (req, res) => {
  console.log('not rendering auth page');
});
Run Code Online (Sandbox Code Playgroud)

// 所有常规路线都使用通用引擎

app.get('*', (req, res) => {
  res.render('index', { req });
});
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。

  • 您需要返回非渲染路由的index.html文件,使用类似:`app.get(routePattern, (req, res) => { res.sendFile(distFolder + '/index.html'); } );` (3认同)
  • @Santosh您可以使用正则表达式来组合 /app/** 和 /auth/** `app.get(/^\/(app | auth)\/(.+)/, function (req, res, next) {}` (2认同)