In angular2, I have a component that does not require a template, but I still get a template error

This is the error I get:

No template for PageNotFoundComponent component Error: No template specified for PageNotFoundComponent component in DirectiveNormalizer.normalizeDirective

This module / component redirects the user to the main page and registers them if the route does not exist, for example. **

This is my component:

import { Directive} from '@angular/core'; import { Router } from '@angular/router'; import { AuthService } from '../../services/authService/authService'; @Directive({ selector: 'PageNotFoundComponent' }) export class PageNotFoundDirective { constructor(_authService: AuthService, _router: Router){ _authService.isAuthenticated().then(() => { _router.navigate(['/home']); }); } } 

This is my module.

 import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { RouterModule } from '@angular/router'; import { PageNotFoundDirective } from './pageNotFound.directive'; import { AuthService } from '../../services/authService/authService'; @NgModule({ imports: [CommonModule, RouterModule], declarations: [PageNotFoundDirective], exports: [PageNotFoundDirective], providers: [AuthService] }) export class PageNotFoundModule { } 

These are my routes:

 import { Route } from '@angular/router'; import { PageNotFoundDirective } from './index'; export const PageNotFoundRoutes: Route[] = [ { path: '**', component: PageNotFoundDirective } ]; 

This is the current error I get:

(index): 95 Error: (SystemJS) Unable to compile 'PageNotFoundDirective' because it is not a component. Error: Failed to compile 'PageNotFoundDirective' because it is not a component.

+6
source share
2 answers

Update

You need a component for the route (the directive cannot be used here)

 @Component({ selector: 'PageNotFoundComponent', template: '' }) 

original

No components without templates in Angular2

Edit

  @Component({ 

to

  @Directive({ 

and you should get what you want.

Components are view directives.

+17
source

Alternatively, if you still want to have a component without a template, you can remove it from the declaration array in the module and use it as a directive

0
source

Source: https://habr.com/ru/post/1259175/


All Articles