将自定义元素和属性添加到编译器模式

Add custom elements and attributes to compiler schema

组件模板中有一些自定义元素和属性(在本例中它们被第三方非Angular代码使用):

<foo></foo>
<div data-bar="{{ bar }}"></div>

它们会导致编译器错误:

Template parse errors:
'foo' is not a known element:
1. If 'foo' is an Angular component, then verify that it is part of this module.
2. If 'foo' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("
    [ERROR ->]<foo></foo>
    <div data-bar="{{ bar }}"></div>
  "): App@1:4
Can't bind to 'bar' since it isn't a known property of 'div'. ("
    <foo></foo>
    <div [ERROR ->]data-bar="{{ bar }}"></div>
  ")
...

如何将 foo 元素和 data-bar 属性添加到编译器模式?

NO_ERRORS_SCHEMA 不是一个选项,因为不希望将其他未知元素和属性列入白名单。

您可以像这样尝试覆盖 DomElementSchemaRegistry

import { DomElementSchemaRegistry, ElementSchemaRegistry } from '@angular/compiler'
import { SchemaMetadata } from '@angular/core';

const MY_DOM_ELEMENT_SCHEMA = [
  'foo'
];

const MY_CUSTOM_PROPERTIES_SCHEMA = {
  'div': {
    'bar': 'string'
  }
};

export class CustomDomElementSchemaRegistry extends DomElementSchemaRegistry {
  constructor() {
    super();
  }

  hasElement(tagName: string, schemaMetas: SchemaMetadata[]): boolean {
    return MY_DOM_ELEMENT_SCHEMA.indexOf(tagName) > -1 || 
         super.hasElement(tagName, schemaMetas);
  }

  hasProperty(tagName: string, propName: string, schemaMetas: SchemaMetadata[]): boolean {
    const elementProperties = MY_CUSTOM_PROPERTIES_SCHEMA[tagName.toLowerCase()];
    return (elementProperties && elementProperties[propName]) || 
        super.hasProperty(tagName, propName, schemaMetas);
  }
}

platformBrowserDynamic().bootstrapModule(AppModule, {
  providers: [{ provide: ElementSchemaRegistry, useClass: CustomDomElementSchemaRegistry }]
});

Plunker Example

看起来不错,但请注意在您的 Plunker 示例中,标记中的 div 如下所示:

data-bar="{{ bar }}"

bar being = "test" 在 dom 上输出,就像:

<div></div>

而不是

<div data-bar="test"></div>

如果您想要呈现属性 - 例如对于聚合物组件,您可以使用以下内容。

[attr.data-bar]="bar" in the markup

注入 CustomDomElementSchemaRegistry 现在的工作方式有点不同:

JIT 编译器

注入器自上次回答以来发生了变化,因此类型签名变化很小。 从版本 5 StaticInjector 需要 deps 数组和 useClass (Angular 8.2)

platformBrowserDynamic().bootstrapModule(AppModule, 
 {
   providers: [
    { 
       provide: ElementSchemaRegistry, 
       useClass: CustomDomElementSchemaRegistry, 
       deps: [] 
    }
   ]
 }
);

AOT编译器

我们可以使用注入器进行提前编译,但是有可能"monkey-patch" class DomElementSchemaRegistry它由编译器实例化,有关详细说明,请在此处查看:

https://medium.com/angular-in-depth/angular-elementschemaregistry-for-dummies-83d54cd31478