有时,我使用Mixins注入重复的函数,如slugUrl()。
但是它不适用于角4编译器。
export function Mixin(decorators: Function[]) {
return function (classFn: Function) {
decorators.forEach(decorator => {
Object.getOwnPropertyNames(decorator.prototype).forEach(name => {
classFn.prototype[name] = decorator.prototype[name];
});
});
};
}
@Mixin([BehaviorInjected])
export class FooComponent {
}如果编译此代码,编译器将抛出:
属性'ngClassControl‘在'FooComponent’类型上不存在。
有什么想法吗?
编辑:由于有人问,这里有另一个例子使用TS混音复制问题,这一次在模板级别。
组件:
@Component({
selector: 'home-page',
template: '<test [tag]="tag"></test>'
})
export class HomePageComponent extends TaggedComponent(MyComponent) {
public tag = 'hi there';
}
@Component({
selector: 'test',
template: '<div></div>'
})
export class TestComponent extends TaggedComponent(MyComponent) {}Mixins:
type Constructor<T> = new(...args: any[]) => T;
export function TaggedComponent<T extends Constructor<{}>>(Base: T) {
class TaggedBase extends Base {
@Input() tag: string;
};
return TaggedBase;
}
export class MyComponent {
protected subscriptions: Subscription = new Subscription();
// ...
}错误:
错误错误:模板解析错误:无法绑定到“标记”,因为它不是“test”的已知属性。(“tag=”标签“>”)
发布于 2017-10-18 12:57:16
这里的主要问题是角编译器功能有限。(请参阅医生们中的更多内容)
AOT编译器使用MetadataCollector生成的元数据。它使用类型记录对象模型(Node树)(,这就是为什么AOT只能与类型记录一起使用)来收集生成ngfactory (在某些情况下也是ngsummary)文件所必需的所有信息。
对于AOT编译器,您提供的示例完全不同:
1)自定义装潢师
@Mixin([BehaviorInjected])
export class FooComponent {}角MetadataCollector将在FooComponent符号元数据( decorators数组中的项)中包含@Mixin装饰符,但是当decorators StaticReflector调用simplify时,它会将被跳过,因为Mixin装饰器没有在只包含严格定义的装饰器(源代码)的特殊地图中注册。
此外,如果我们甚至将它包含在该映射中,那么它仍然不会在aot编译期间执行,因为它只适用于受支持的装饰器。
2)调用自定义函数
export class HomePageComponent extends TaggedComponent(MyComponent) {MetadataCollector 将添加 TaggedComponent将元数据收集作为像{__symbolic: 'error', message: 'Symbol reference expected'};这样的符号,但它在StaticReflector中也有将被跳过。
据我所知,目前还没有解决办法来支持它。
发布于 2017-10-17 17:02:10
请参阅https://github.com/angular/angular/issues/19145
我相信这是同样的问题。装饰器继承是中断的混合体,所以目前您将不得不重复装饰属性。
https://stackoverflow.com/questions/43679638
复制相似问题