在Angular2中,我有一个不需要模板的组件,但我仍然得到一个模板错误

在Angular2中,我有一个不需要模板的组件,但我仍然得到一个模板错误,angular,Angular,这是我得到的错误: 没有为组件PageNotFound指定模板组件错误:否 为组件PageNotFoundComponent指定的模板 at DirectiveNormalizer.normalizeDirective 此模块/组件将用户重定向到主页,并在路由不存在时将其注销,例如** 这是我的组件: import { Directive} from '@angular/core'; import { Router } from '@angular/router'; import { Auth

这是我得到的错误:

没有为组件PageNotFound指定模板组件错误:否 为组件PageNotFoundComponent指定的模板 at DirectiveNormalizer.normalizeDirective

此模块
/
组件将用户重定向到主页,并在路由不存在时将其注销,例如
**

这是我的组件:

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']);
     });
  }
}
import { Route } from '@angular/router';
import { PageNotFoundDirective } from './index';

export const PageNotFoundRoutes: Route[] = [
  {
    path: '**',
    component: PageNotFoundDirective
  }
];
这是我的模块

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 { }
这是我的路线:

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']);
     });
  }
}
import { Route } from '@angular/router';
import { PageNotFoundDirective } from './index';

export const PageNotFoundRoutes: Route[] = [
  {
    path: '**',
    component: PageNotFoundDirective
  }
];
这是我得到的当前错误:

(索引):95错误:(SystemJS)无法编译“PageNotFoundDirective” 因为它不是一个组件。错误:无法编译 “PageNotFoundDirective”,因为它不是组件


更新

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 { }
对于管线,您需要一个组件(此处不能使用指令)

原创

Angular2中没有没有没有模板的组件

改变

 @Component({

你应该得到你想要的


组件是带有视图的指令。

PageNotFoundModule代码可以放在
AppComponent
中,在那里你不能创建没有模板的组件我已经将我的代码更新为一个指令我已经做了更改,现在我得到了这个错误:ReferenceError:指令未定义
从“@angular/core”导入{Directive}
您不能在路由中使用指令。对于这种情况,您需要使用空模板创建组件。您可以使用
@Component({template:''})
我需要它来创建路由。我给你们看了我上面的路线。我刚看到它——一开始就错过了那个部分。只需使用一个组件并将
模板:'
添加到
@组件(…)
装饰器。