angular - Angular 6 生产版本“无法绑定到‘禁用’,因为它不是‘div’的已知属性”
问题描述
我的应用程序在使用 JIT 编译器时似乎可以工作,但是当我尝试使用 AOT 编译器时,ng build --prod
它会引发错误:
ERROR in : Can't bind to 'disabled' since it isn't a known property of 'div'. ("[ERROR ->]<div formButton></div>")
我如何弄清楚这个错误来自哪里?
我只是添加了延迟加载的功能模块,而不是导入其中的所有内容,app.module.ts
我不知道是否需要导入FormButtonComponent
功能模块?
我搜索了代码库,formButton
除了button
.
这是button.component.ts
:
import { Component, HostBinding, Input, OnChanges, SimpleChanges } from "@angular/core";
/**
* Button with loading and disabled states
*/
@Component({
moduleId: module.id,
selector: '[formButton]',
templateUrl: 'button.component.html'
})
export class FormButtonComponent implements OnChanges {
@Input() loading? = false;
@Input() disabled? = false;
@HostBinding('disabled') disabledAttribute = false;
ngOnChanges(changes: SimpleChanges): void {
this.updateState();
}
updateState(): void {
this.disabledAttribute = this.disabled || this.loading;
}
}
button.component.html
<ng-content></ng-content>
<spinner [size]="small" *ngIf="loading"></spinner>
以下是我应用程序中其他地方的一些示例调用:
<button formButton [loading]="template.loading" [disabled]="disabled" class="button" type="submit">Design</button>
<button formButton class="button" [loading]="form.disabled" (click)="save()">Change Password</button>
<button formButton [ngClass]="buttonClass" (click)="upload()" [loading]="uploaderWrapper.isUploading">{{ buttonText }}</button>
解决方案
编辑:我与一位 Angular 团队成员交谈,这是由于我的选择器没有指定元素。因此,当使用ng build --prod
Angular构建div
作为“最小公分母”时:
您看到的错误指的是因为 div 是 HTML 元素的最低公分母。如果一个组件选择器没有指定任何其他元素类型,那么它就变成了一个 div。
所以有两种解决方案:
1) 使选择器更具体,以便它只尝试使用具有该disabled
属性的元素进行 AOT 编译。
更新selector: '[formButton]'
到selector: 'button[formButton]'
@Component({
moduleId: module.id,
selector: 'button[formButton]',
templateUrl: 'button.component.html'
})
2) 更新为使用attr.disabled
而不是disabled
. 这样做的缺点是你必须使用value | null
而不是true | false
因为浏览器寻找disabled
属性的存在而不是它的值。
@HostBinding('disabled') disabledAttribute = false;
至
@HostBinding('attr.disabled') disabledAttribute = false;
使用此模板:
<ng-content></ng-content>
<spinner [size]="small" *ngIf="loading"></spinner>
推荐阅读
- c# - xamarin.forms nuget 包兼容性问题
- ios - 如何修复 Thread 1: EXC_BAD_ACCESS (code=1, address=0x52879984) 在我的 appdelegate 上?
- perl - 如何在 Ubuntu 上安装 Perl-5.8.9
- c++ - 什么是“结构黑客”和“类型/非类型隐藏”?
- swift - 通过邮递员和 URLSession 获取的数据在这里不同 http://173.249.20.137:9000/apiapp/coupon
- javascript - 导入组件时使用 JSX 错误时,get React' 必须在范围内
- c++ - stderr 和 stdout 的行为不同
- mysql - 将 MATCH 与 OR 一起使用会返回不满足任何条件的结果
- c++ - 如何将父小部件焦点重定向到子小部件?
- android - ImageView 没有形成卡片视图,在它工作正常之前,现在出了点问题,我无法弄清楚