Est*_*ask 3 angular2-compiler angular4 angular
组件模板中有一些自定义元素和属性(在此示例中,第三方非角度代码使用了它们):
<foo></foo>
<div data-bar="{{ bar }}"></div>
Run Code Online (Sandbox Code Playgroud)
它们会导致编译器错误:
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>
")
...
Run Code Online (Sandbox Code Playgroud)
如何将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 }]
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
746 次 |
| 最近记录: |