在组件的ngOnInit方法中,@Input个值将被绑定,因此您可以判断组件上的那些属性,但似乎没有办法判断@Output个事件绑定.我想知道@Output是否连接在组件上.

(使用Angular 和字体)

import {Component, Output, EventEmitter} from '@angular/core';

@Component({
    selector: 'sample',
    template: `<p>a sample</p>`
})
export class SampleComponent {
    @Output() cancel = new EventEmitter();
    
    ngOnInit() {
        // would like to check and see if cancel was used
        // on the element <sample (cancel)="doSomething()"></sample> 
        // or not <sample></sample>
    }
}

推荐答案

方法与user1448982相同,但不使用ObservableWrapperObservableWrapper是指不通过api公开使用的平台代码.

(Using Angular 2 RC1 and TypeScript)
Note: This only started working from 2.0.0-beta.16 and greater

import {Component, Output, EventEmitter} from '@angular/core';

@Component({
    selector: 'sample',
    template: `<p>a sample</p>`
})
export class SampleComponent {
    @Output() cancel = new EventEmitter();
    private isCancelUsed = false;

    ngOnInit() {
        this.isCancelUsed = this.cancel.observers.length > 0;
    }
}

Plunker

ObservableWrapper.hasSubscribers方法在内部执行这一行,所以您可以在这里执行相同的操作.

When using TypeScript you will get a transpile time error if Angular ever ends up changing the 100 from a 101 (which is part 102, thus the 103 property).

Typescript相关问答推荐

如何使用TypScript限制允许对JavaScript值进行的操作集?

与Prettify助手类型中的对象相交?

Typescript自定义键映射

如何使用TypScript和react-hook-form在Next.js 14中创建通用表单组件?

类型脚本中的Gnomeshell 扩展.如何导入.ts文件

使用React处理Websocket数据

Angular中的其他服务仅获取默认值

属性的类型,该属性是类的键,其值是所需类型

Webpack说你可能需要在Reactjs项目中增加一个加载器''

如何将联合文字类型限制为文字类型之一

泛型类型,引用其具有不同类型的属性之一

如何在单击停止录制按钮后停用摄像机?(使用React.js和Reaction-Media-Recorder)

与字符串文字模板的结果类型匹配的打字脚本

Fp-ts使用任务的最佳方法包装选项

对有效枚举值的常量字符串进行常规判断

使用Type脚本更新对象属性

有没有办法传递泛型类型数组,以便推断每个元素的泛型类型?

类型实例化过深,可能是无限的&TypeScrip中的TupleUnion错误

Typescript泛型调用对象';s方法

无法使用prisma中的事务更新记录