继承 Angular 5 组件并覆盖装饰器属性

2023-12-04

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

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

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

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

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

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

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


最后我想到了自定义装饰器的实现(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):

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();
    }
}

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

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 {
}

注意:这没有正式记录,并且在许多情况下可能不起作用。我希望它对其他人有帮助,但使用它的风险由您自己承担。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

继承 Angular 5 组件并覆盖装饰器属性 的相关文章

随机推荐

  • 单击链接不应触发家长 onclick 事件

    我有以下代码 div some text a href asd php click a div 当有人点击链接时 就会触发 javaschipt 事件 我希望该事件仅在有人单击文本或 div 容器内的空白区域时触发 如果有人点击链接则不会
  • 使用 cx-freeze 时没有名为 bs4 的模块

    我正在尝试使用以下命令将我的 python 脚本转换为适用于 Windows 的独立可执行文件CX 冻结 为此 我刚刚输入了这个命令cxfreeze PlaylistDownloader py PlaylistDownloader py 是
  • 如何在具有枚举字段的实体上使用 JPA CriteriaQuery 填充 DTO 类字符串字段?

    我正在使用 JPA Criteria API 在查询多选中我想检索枚举属性 而不是枚举本身 这是我的查询 final CriteriaQuery
  • Swift + didUpdateUserLocation 没有被调用

    我无法打电话MKMapView委托方法didUpdateUserLocation 到目前为止我做了什么 添加框架MapKit framwork在项目中 在视图控制器中导入框架import MapKit line 在plist文件中添加密钥
  • Jersey 在尝试返回 XML 响应时返回 500

    我正在尝试使用基于 Jersey 2 12 创建自己的 RESTful WS 应用程序this文章 我想根据从 url 传递的 id 返回一个类的 XML 表示形式 但是 在尝试从以下任一位置尝试时 我收到了 500 响应代码高级休息客户端
  • 如何获取 XMLHttpRequest 中的响应 url?

    有一个页面 url 我通过 XMLHttpRequest 请求它 但是我没有从请求的 url 中得到响应 它将请求定向到另一个页面 请求 gt page php 得到回复 gt 定向页面 php 问题是如何获取响应 url 示例中的dire
  • 多个 CSS 计数器未按预期工作

    我正在尝试在 html 表中创建多个级别的计数器 但这并不像我预期的那样工作 第一个计数器工作正常 但接下来的计数器不工作 不知何故 计数器没有增加或重置错误 代码
  • Vue3 国际化与 I18n 和脚本设置 (vite)

    我正在尝试将我的 Vue3 带有 Vite 设置 项目国际化 intlify vite plugin vue i18n我正在使用
  • 为什么setup.py在安装之前会扫描命名空间的内容?

    我使用带有 setuptools 的命名空间在两个不同的存储库中分发相同的模块 目标是得到mymodule one and mymodule two安装后 知道内容one and two来自不同的仓库 但看起来像两个setup py互相扫内
  • 将 System.Double 表示为可排序字符串的最佳方式是什么?

    在所有基础类型都是字符串的数据格式中 数字类型必须转换为可以按字母顺序比较的标准化字符串格式 例如 一个short对于价值27可以表示为00027如果没有负面影响 代表一个的最好方式是什么double作为字符串 就我而言 我可以忽略负数 但
  • 背景附件:固定;不使用背景位置

    我做了一个codepen解释我的问题 当用户滚动时 蓝色图像应跟随用户滚动 蓝色图像应粘贴在旁边部分的另一侧 右侧为左侧 左侧为右侧 pb 是这样的 background attachment fixed 这个CSS规则不起作用 backg
  • EnableAutoRejoiningGroups 在 SignalR 1.0 中消失

    看起来 GlobalHost HubPipeline EnableAutoRejoiningGroups 已在 SignalR 1 0 最终版本中消失 然而 发行说明中没有提及它的去向 也没有任何线索如何恢复此功能 有人知道发生了什么事吗
  • 了解 C 中的静态变量声明/初始化

    我的项目中只有一个名为 test c 的文件 如果我不定义 TRUE 下面的代码将无法编译 我用的是vc 我只是想了解这种行为 请阐明这方面的情况 ifdef TRUE static int a static int a 1 else st
  • 如何使用正则表达式 String.match() 区分 ${SOME_TEXT}

    我需要这个字符串 var x Hi name How are you name you are old name share with other how do u feel 我需要使用正则表达式知道有多少不同的 ANY THING 存在
  • 为什么非侵入式序列化要添加 5 字节零前缀?

    我正在研究使用 boost archive 的应用程序中从非标准字符串到标准字符串的端口 非标准字符串的 反 序列化以非侵入式方式定义 如下例所示 序列化和反序列化按预期工作 但是当移植的应用程序收到旧消息时 它会因分配错误而崩溃 这是由于
  • opencv 的 Python/Kivy 相机小部件错误

    我一直在尝试制作一个打开设备相机的应用程序 但出现此错误 CRITICAL Camera Unable to find any valuable Camera provider at all videocapture ImportError
  • 导入错误:无法导入名称“_imagingtk”

    我正在使用 Anaconda 和 python 3 4 但我无法在 Windows 8 1 上获得我需要的所有枕头包 我通过 Anaconda 控制台安装了枕头 pip install pillow 这导致 The following pa
  • 无法在 Anaconda 中安装聊天机器人

    安装时chatterbot在 Anaconda 中使用 Python 3 7 我收到以下错误 找到现有安装 PyYAML 3 13 无法卸载 PyYAML 它 是一个 distutils 安装项目 因此我们无法准确地 确定哪些文件属于它 这
  • 如何在 PL/pgSQL 中按行类型返回表

    我正在尝试使用 PL pgSQL PostgreSQL 9 3 实现一个函数 该函数返回与参数中的输入表具有相同结构的表 基本上 我想更新一个表 并使用 plpgsql 返回更新后的表的副本 我搜索了一下 发现了几个相关的问题 例如从 PL
  • 继承 Angular 5 组件并覆盖装饰器属性

    在 Angular 2 4 中 我们可以创建自定义装饰器来扩展父组件 装饰器属性的实际重写是根据需要在自定义装饰器中处理的 为了获取父注释 我们使用了 let parentAnnotations Reflect getMetadata an