Skip to content

Commit 6ccdbb1

Browse files
committed
Пример реализации 404 страницы
1 parent 4ee7459 commit 6ccdbb1

File tree

9 files changed

+101
-1
lines changed

9 files changed

+101
-1
lines changed

src/app/app.routing.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { MetaGuard } from '@ngx-meta/core';
33

44
const routes: Routes = [
55
{
6-
path: '', loadChildren: './home/home.module#HomeModule', pathMatch: 'full',
6+
path: '', loadChildren: './home/home.module#HomeModule',
77
data: {
88
// for override default meta
99
meta: {
@@ -19,6 +19,9 @@ const routes: Routes = [
1919
{ path: 'mock', loadChildren: './mock-server-browser/mock-server-browser.module#MockServerBrowserModule' },
2020
// with meta
2121
{ path: 'back', loadChildren: './transfer-back/transfer-back.module#TransferBackModule', canActivateChild: [MetaGuard]},
22+
// 404
23+
{ path: '404', loadChildren: './not-found/not-found.module#NotFoundModule' },
24+
{ path: '**', redirectTo: '404', pathMatch: 'full' }
2225
];
2326
// must use {initialNavigation: 'enabled'}) - for one load page, without reload
2427
export const AppRoutes = RouterModule.forRoot(routes, { initialNavigation: 'enabled' });
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>
2+
not-found works!
3+
</p>

src/app/not-found/not-found.component.scss

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { NotFoundComponent } from './not-found.component';
4+
5+
describe('NotFoundComponent', () => {
6+
let component: NotFoundComponent;
7+
let fixture: ComponentFixture<NotFoundComponent>;
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [ NotFoundComponent ]
12+
})
13+
.compileComponents();
14+
}));
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(NotFoundComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { NotFoundService } from './not-found.service';
3+
4+
@Component({
5+
selector: 'app-not-found',
6+
templateUrl: './not-found.component.html',
7+
styleUrls: ['./not-found.component.scss']
8+
})
9+
export class NotFoundComponent implements OnInit {
10+
public status: { code: number, message: string };
11+
constructor(private _notFoundService: NotFoundService) {}
12+
13+
ngOnInit() {
14+
this._notFoundService.setStatus(404, 'Not Found');
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NgModule } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
import { NotFoundComponent } from './not-found.component';
4+
import { NotFoundRoutes } from './not-found.routing';
5+
import { NotFoundService } from './not-found.service';
6+
7+
@NgModule({
8+
imports: [
9+
CommonModule,
10+
NotFoundRoutes
11+
],
12+
providers: [NotFoundService],
13+
declarations: [NotFoundComponent]
14+
})
15+
export class NotFoundModule { }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { NotFoundComponent } from './not-found.component';
2+
import { Routes, RouterModule } from '@angular/router';
3+
4+
const routes: Routes = [
5+
{ path: '' , component: NotFoundComponent},
6+
];
7+
8+
export const NotFoundRoutes = RouterModule.forChild(routes);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { TestBed, inject } from '@angular/core/testing';
2+
3+
import { NotFoundService } from './not-found.service';
4+
5+
describe('NotFoundService', () => {
6+
beforeEach(() => {
7+
TestBed.configureTestingModule({
8+
providers: [NotFoundService]
9+
});
10+
});
11+
12+
it('should be created', inject([NotFoundService], (service: NotFoundService) => {
13+
expect(service).toBeTruthy();
14+
}));
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {RESPONSE} from '@nguniversal/express-engine/tokens';
2+
import {Inject, Injectable, Optional} from '@angular/core';
3+
4+
@Injectable()
5+
export class NotFoundService {
6+
7+
constructor(@Optional() @Inject(RESPONSE) private _response: any) {}
8+
9+
public setStatus(_code: number, _message: string): void {
10+
if (this._response) {
11+
this._response.statusCode = _code;
12+
this._response.statusMessage = _message;
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)