简体   繁体   English

Angular 结构指令在 NPM 中不起作用

[英]Angular Structural Directives is not working from NPM

  1. I've created npm package sezam-shareds我创建了 npm package sezam-shareds
  2. Install package to new Project将 package 安装到新项目
  3. Added component from this package: <sezam-overflow [show]="true"></sezam-overflow> to component in project将此 package: <sezam-overflow [show]="true"></sezam-overflow>组件添加到项目中的组件
  4. An error occurs in browser console: core.js:9847 NG0303: Can't bind to 'ngIf' since it isn't a known property of 'div'浏览器控制台出现错误: core.js:9847 NG0303: Can't bind to 'ngIf' since it isn't a known property of 'div'

Content of html in package: package中html的内容:

<div *ngIf="show">
...
</div>

Content of html in project:项目中html的内容:

<sezam-overflow [show]="true"></sezam-overflow>

Component in package: package 中的组件:

import { Component, Input, Output, EventEmitter, OnChanges, forwardRef } from '@angular/core';

@Component({
  selector: 'sezam-overflow',
  templateUrl: `sezam-overflow.component.html`,
})
export class SezamOverflowComponent {
  @Input() show = false;

  constructor() {}
}

How fix error?如何修复错误? Can't bind to 'ngIf'

If you would like to install a package, you need to add postintall script to package.json: "postinstall": "ngcc"如果您想安装 package,您需要将 postintall 脚本添加到 package.json: "postinstall": "ngcc"

this error is not related to the component.此错误与组件无关。 just add the below code to your module只需将以下代码添加到您的模块

import { CommonModule } from '@angular/common'; 
import { BrowserModule } from '@angular/platform-browser';

your final code like this你的最终代码是这样的

 @NgModule({
    imports: [CommonModule,BrowserModule ],
    declarations: [SezamOverflowComponent]
})
class AppModule {}

4 things to check:要检查的 4 件事:

  1. What module declares the SezamOverflowComponent ?哪个模块声明了SezamOverflowComponent
  2. Does this modules exports contain the SezamOverflowComponent ?此模块exports是否包含SezamOverflowComponent (I'll just assume you called this SezamOverflowModule ...) (我假设您将其称为SezamOverflowModule ...)
  3. What component are you using the <sezam-overflow> on?您在哪个组件上使用<sezam-overflow> What module declares this component?什么模块声明了这个组件? (Assume XxMidule...) (假设 XxMidule ...)
  4. This XxMidule should import the SezamOverflowModule这个XxMidule应该导入SezamOverflowModule

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

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