Angular 8-无法使用指令-无法绑定,因为它不是已知属性

问题描述

我创建了一个非常简单的指令(直接从文档中进行操作即可),但是,我不断收到此错误:

Uncaught Error: Template parse errors:
Can't bind to 'appHasPermission' since it isn't a known property of 'button'. ("
                (click)="navigateToSingleCompany()"
                [translate]="'register.text.company'"
                [ERROR ->]*appHasPermission="true"
            >
                Company
"): ng:///DashboardModule/CompaniesOverviewCardComponent.html@41:4
Property binding appHasPermission not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations".

指令是我的共享模块:

import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { NavMenuComponent } from "./layout/nav-menu/nav-menu.component";
import { CoreModule } from "../core/core.module";
import { RouterModule } from "@angular/router";
import { SideNavComponent } from "./layout/side-nav/side-nav.component";
import { RegisterCompanyFormComponent } from "./components/register-company-form/register-company-form.component";
import { ReactiveFormsModule } from "@angular/forms";
import { ErrorCardComponent } from "./components/error-card/error-card.component";
import { HasPermissionDirective } from "./directives/has-permission.directive";

@NgModule({
    declarations: [
        NavMenuComponent,SideNavComponent,RegisterCompanyFormComponent,ErrorCardComponent,HasPermissionDirective,],imports: [CommonModule,CoreModule,RouterModule,ReactiveFormsModule],exports: [
        NavMenuComponent,CommonModule,})
export class SharedModule {}

指令现在看起来像这样:

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

@Directive({
    selector: "[appHasPermission]",})
export class HasPermissionDirective {
    private hasView = false;

    constructor(
        private templateRef: TemplateRef<any>,private viewContainer: ViewContainerRef
    ) {}

    @Input() set hasPermission(condition: boolean) {
        if (!condition && !this.hasView) {
            this.viewContainer.createEmbeddedView(this.templateRef);
            this.hasView = true;
        } else if (condition && this.hasView) {
            this.viewContainer.clear();
            this.hasView = false;
        }
    }
}

它被这样称呼:

        <button
            class="btn btn-dark"
            (click)="navigateToSingleCompany()"
            [translate]="'register.text.company'"
            *appHasPermission="true"
        >
            Company
        </button>

我在共享模块的声明和导出中添加了指令,并且已将共享模块导入到我使用的所有其他模块中。我不知道该怎么办。

解决方法

问题在于选择器名称与输入名称不匹配,这样可以使其起作用:

    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
,

如果您使用不带'*'前缀的指令应该没问题:

<button
        class="btn btn-dark"
        (click)="navigateToSingleCompany()"
        [translate]="'register.text.company'"
        appHasPermission [hasPermission]="true"
    >
        Company
    </button>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...