Den*_*eev 4 search-engine angular-universal angular
我已经使用Universal发布了我的项目,并在.htaccess中指定了所有请求都应转到index.html(Angular应用程序的根页面)
正如在这里被问到的那样:https : //angular.io/guide/universal
它允许共享链接并打开URL中指定的组件
另外,如果打开了错误的路线,则创建一个组件: 使用Angular2处理404
export const routes:RouterConfig = [
...Routes,
// Show the 404 page for any routes that don't exist.
{ path: '**', component: Four04Component }
];
Run Code Online (Sandbox Code Playgroud)
问题是搜索引擎将Four04Component视为状态为200 OK的简单页面,而不是错误页面。您知道如何与Four04Component一起检索404错误吗?
小智 5
您必须将Response注入到您的angular应用程序中,以实现首先更改您的以下行server.ts:
app.get('*', (req, res) => { //you can find these line easily
res.render('index', {
req: req,
res: res,
providers: [
{
provide: REQUEST, useValue: (req)
},
{
provide: RESPONSE, useValue: (res)
}
]
});
});
Run Code Online (Sandbox Code Playgroud)
然后在您的Four04组件中注入如下响应:
constructor(@Optional() @Inject(RESPONSE) private response: Response,
@Inject(PLATFORM_ID) private platformId: Object) {}
Run Code Online (Sandbox Code Playgroud)
之后,只需在Four04组件的ngoninit上尝试以下操作:
ngOnInit(){
if(isPlatformServer(this.platformId)){
this.response.status(404);
}
}
Run Code Online (Sandbox Code Playgroud)
希望这些对某人有帮助。
| 归档时间: |
|
| 查看次数: |
518 次 |
| 最近记录: |