Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

angular - 在Angular2中,我有一个不需要模板的组件,但仍然收到模板错误(In Angular2 I have a component that doesnt require a template but I still get a template error)

This is the error I get:

(这是我得到的错误:)

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

(没有为组件PageNotFoundComponent指定模板,错误:没有在DirectiveNormalizer.normalizeDirective为组件PageNotFoundComponent指定模板。)

This module / component redirect user to homepage and logs them out if route doesnt exist eg **

(该模块/组件将用户重定向到主页,如果路由不存在则将其注销,例如**)

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 { }

This is 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) Could not compile 'PageNotFoundDirective' because it is not a component.

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

Error: Could not compile 'PageNotFoundDirective' because it is not a component.

(错误:无法编译“ PageNotFoundDirective”,因为它不是组件。)

  ask by AngularM translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

update

(更新)

For a route you need a component (a directive can't be used here)

(对于路由,您需要一个组件(此处不能使用指令))

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

original

(原版的)

There are no components without templates in Angular2

(Angular2中没有模板就没有组件)

Change

(更改)

 @Component({

to

(至)

 @Directive({

and you should get what you want.

(你应该得到你想要的。)

Components are directives with a view.

(组件是带有视图的指令。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...