本文介绍了仅限某些路线的角度通用渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was playing with angular universal a bit but cant find option to use server side rendering only for some pages like home page and render all other routes in standard angular way. I don't want to use server side rendering for private pages where SEO is not needed. I can configure routes in express like this

// 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);

理想情况下,我根本不想了解NodeJs并在角度路由配置上配置属性喜欢serverSide:true

Ideally I don't want to know about NodeJs at all and configure it on angular routes config with property like 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 },
];


推荐答案

在你的server.ts
中为路由你不想渲染就像下面这样做

In your server.ts for routes you don't want to render just do as below

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');
});

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

// All regular routes use the Universal engine

app.get('*', (req, res) => {
  res.render('index', { req });
});

希望这有帮助。

这篇关于仅限某些路线的角度通用渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 01:30