继承 Angular 5 组件并覆盖装饰器属性 [英] Inheritance of Angular 5 components with overriding the decorator properties

查看:22
本文介绍了继承 Angular 5 组件并覆盖装饰器属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Angular 2/4 中,我们可以创建自定义装饰器来扩展父组件.装饰器属性的实际覆盖是根据需要在自定义装饰器中处理的.要获取我们使用的父注释:

In Angular 2/4 we could create custom decorator for extending parent component. Actual overriding of the decorator properties was handled as needed in the custom decorator. To get parent annotations we used:

let parentAnnotations = Reflect.getMetadata('annotations', parentTarget);

更新到 Angular 5 后,这不再起作用.关于这个我们可以使用的答案:

After update to Angular 5 this doesn't work anymore. Regarding this answer we could use:

target['__annotations__'][0] 用于获取父组件注释.

target['__annotations__'][0] for getting parent component annotations.

为了在 Angular 2/4 的当前组件中设置注释,我们使用了:

In order to set annotations in the current component in Angular 2/4 we used:

let metadata = new Component(annotation);Reflect.defineMetadata('annotations', [ metadata ], target);

如何在 Angular 5 中设置当前组件注释?

How can set current component annotations in Angular 5?

推荐答案

最后我想到了自定义装饰器(extendedcomponent.decorator.ts)的这个实现:

At the end I came up to this implementation of a custom decorator (extendedcomponent.decorator.ts):

import { Component } from '@angular/core';

export function ExtendedComponent(extendedConfig: Component = {}) {
    return function (target: Function) {
        const ANNOTATIONS = '__annotations__';
        const PARAMETERS = '__paramaters__';
        const PROP_METADATA = '__prop__metadata__';

        const annotations = target[ANNOTATIONS] || [];
        const parameters = target[PARAMETERS] || [];
        const propMetadata = target[PROP_METADATA] || [];

        if (annotations.length > 0) {
            const parentAnnotations = Object.assign({}, annotations[0]);

            Object.keys(parentAnnotations).forEach(key => {
                if (parentAnnotations.hasOwnProperty(key)) {
                    if (!extendedConfig.hasOwnProperty(key)) {
                        extendedConfig[key] = parentAnnotations[key];
                        annotations[0][key] = '';
                    } else {
                        if (extendedConfig[key] === parentAnnotations[key]){
                             annotations[0][key] = '';
                        }
                    }
                }
            });
        }
        return Component(extendedConfig)(target);
    };
}

示例用法:

首先照常实现父组件(myparent.component.ts):

First implement the parent component as usual (myparent.component.ts):

import { Component, Output, EventEmitter, Input } from '@angular/core';
@Component({
    selector: 'my-component',
    templateUrl: 'my.component.html'
})
export class MyParentComponent implements OnInit {
    @Input() someInput: Array<any>;
    @Output() onChange: EventEmitter<any> = new EventEmitter();

    constructor(
        public formatting: FormattingService
    ) {
    }

    ngOnInit() {

    }

    onClick() {
        this.onChange.emit();
    }
}

然后实现继承父组件的子组件:

After that implement child component which inherit the parent component:

import { Component, OnInit } from '@angular/core';
import { ExtendedComponent } from './extendedcomponent.decorator';
import { MyParentComponent } from './myparent.component';


@ExtendedComponent ({
    templateUrl: 'mychild.component.html'
})

export class MyChildComponent extends MyParentComponent {
}

注意:这没有正式记录,在许多情况下可能不起作用.我希望它会对其他人有所帮助,但使用时风险自负.

Note: This is not officially documented and may not work in many cases. I hope that it will help somebody else, but use it at your own risk.

这篇关于继承 Angular 5 组件并覆盖装饰器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆