简体   繁体   English

使用指令时出现Angular 4错误

[英]Angular 4 Error when using Directives

I have the following directive declaration: 我有以下指令声明:

import {
  Directive,
  Input,
  OnInit,
  TemplateRef,
  ViewContainerRef
} from '@angular/core';

import { UserService } from './services/user.service';

    @Directive({ selector: '[appShowAuthed]' })
    export class ShowAuthedDirective implements OnInit {

      condition: boolean;
      constructor(
        private templateRef: TemplateRef<any>,
        private userService: UserService,
        private viewContainer: ViewContainerRef
      ) {}
      ngOnInit() {
        this.userService.isAuthenticated.subscribe(
          (isAuthenticated) => {
            if (isAuthenticated && this.condition || !isAuthenticated && !this.condition) {
              this.viewContainer.createEmbeddedView(this.templateRef);
            } else {
              this.viewContainer.clear();
            }
          }
        );
      }

      @Input() set showAuthed(condition: boolean) {
        this.condition = condition;
      }
    }

And I have defined it in the declarations of my NgModule as below: 我已经在我的NgModule的声明中定义了它,如下所示:

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { ListErrorsComponent } from './list-errors.component';
import { ShowAuthedDirective } from './show-authed.directive';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    HttpModule,
    RouterModule
  ],
  declarations: [
    ListErrorsComponent,
    ShowAuthedDirective,
  ],
  exports: [
    CommonModule,
    FormsModule,
    ListErrorsComponent,
    ReactiveFormsModule,
    HttpModule,
    RouterModule,
    ShowAuthedDirective
  ]
})
export class SharedModule {}

But when I use it in my html component as below: 但是当我在我的html组件中使用它时,如下所示:

<nav class="navbar navbar-light">
  <div class="container">
    <a class="navbar-brand" routerLink="/">plant-simulator</a>

    <!-- Show this for logged out users -->
    <ul *appShowAuthed="false"
        class="nav navbar-nav pull-xs-right">

      <li class="nav-item">
        <a class="nav-link"
          routerLink="/register"
          routerLinkActive="active">
          Sign Up
        </a>
      </li>

    </ul>
    <ul *appShowAuthed="true"
        class="nav navbar-nav pull-xs-right">
      <!-- Show this for logged in users -->
      <li class="nav-item">
        <a class="nav-link"
          [routerLink]="['/profile', currentUser.id]"
          routerLinkActive="active">
          <img [src]="currentUser.image" *ngIf="currentUser.image" class="user-pic" />
          {{ currentUser.email }}
        </a>
      </li>
    </ul>

  </div>
</nav>

I get some errors as can be seen in the screen shot below: 如下面的屏幕截图所示,我遇到了一些错误:

在此处输入图片说明

Any clues as to why it is complaining about parsing? 关于它为什么抱怨解析的任何线索?

I had to rename the directive to match the class declaration: 我必须重命名指令以匹配类声明:

@Directive({ selector: '[showAuthed]' })

Notice the selector now matches the name of the class ShowAuthedDirective 注意,选择器现在与类ShowAuthedDirective的名称匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM