在使用Angular Material中的mat-error组件时,可能会遇到该组件无法封装在自定义组件中的问题。解决方法如下:
1.使用ng-content将mat-error组件作为子组件在父组件中引用。例如:
2.设置ViewEncapsulation.None,如下所示:
import { Component, ViewEncapsulation } from '@angular/core';
@Component({ selector: 'app-form-field', templateUrl: './form-field.component.html', styleUrls: ['./form-field.component.scss'], encapsulation: ViewEncapsulation.None }) export class FormFieldComponent {}
然后,在自定义组件模板中,不要再使用Encapsulation.None指令,否则会破坏组件封装性。