请告诉我如何使用 SSR Angular Universal 在第 404 页上提供 http 代码 404。Angular 7 版本的代码不起作用,给出了很多无穷无尽的错误。代码如下所示。
//Код компонента страницы 404 (notfound.component.ts)
import {Component, Inject, OnInit, Optional, PLATFORM_ID} from '@angular/core';
import {RESPONSE} from '@nguniversal/express-engine/tokens';
import {isPlatformServer} from '@angular/common';
import { Global } from '../core/global'
@Component({
selector: 'app-not-found',
templateUrl: './not-found.component.html',
styleUrls: ['./not-found.component.css']
})
export class NotFoundComponent implements OnInit {
constructor(
public global: Global,
@Inject(PLATFORM_ID) private platformId: Object,
@Optional() @Inject(RESPONSE) private response,
) {}
ngOnInit() {
if (isPlatformServer(this.platformId)) {
this.response.status(404);
}
}
}
//Часть кода в файле server.ts
import {REQUEST, RESPONSE} from '@nguniversal/express-engine/tokens';
app.get('*', (req, res) => {
res.render('index', {
req,
providers: [
{
provide: RESPONSE,
useValue: res
}
]
});
});
ps如果对如何实现代码的返回有其他的想法和建议,我会很乐意提出建议)
实现Angular 8 - Universal 的 404 代码返回的唯一发现(工作)方法是将代码添加到Nginx Web 服务器的配置文件中
当您直接进入 404 页面时,此方法适用于该页面,但在您重定向到该页面时无效。
https://github.com/patrickmichalina/fusebox-angular-universal-starter/blob/master/src/client/app/shared/services/server-response.service.ts
我想你可以使用上面的代码。
(我们在下一个讨论线程中 - https://github.com/DSpace/dspace-angular/issues/91#issuecomment-318547118)
您需要将 res 传递给 server.ts,而不仅仅是 req。
它对我有用:
没有任何提供者