请告诉我如何使用 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如果对如何实现代码的返回有其他的想法和建议,我会很乐意提出建议)