仅适用于某些路线的角度通用渲染 [英] Angular universal rendering for some routes only
问题描述
我在玩 angular 通用,但找不到仅对某些页面(如主页)使用服务器端渲染并以标准角度方式渲染所有其他路由的选项.我不想对不需要 SEO 的私人页面使用服务器端渲染.我可以像这样在 express 中配置路由
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 });
});
希望这会有所帮助.
这篇关于仅适用于某些路线的角度通用渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!