Angular RxJs主题:获取发射器

Angular RxJs主题:获取发射器,angular,typescript,rxjs,Angular,Typescript,Rxjs,我正在处理主题,在一个类中有一个.subscribe()。我从其他不同的类向这个类发出值。问题是订阅现在被多次触发,我不知道从哪里发射 是否有方法从触发发射(.next)的位置获取类或引用 期望的行为: 在用svc: obs: Subject<Date> = new Subject<Date>(); 类别n: svc.obs.next(new Date()); 订户: svc.obs.subscribe((date) => { console.l

我正在处理主题,在一个类中有一个
.subscribe()
。我从其他不同的类向这个类发出值。问题是订阅现在被多次触发,我不知道从哪里发射

是否有方法从触发发射(
.next
)的位置获取类或引用

期望的行为:

在用svc:

obs: Subject<Date> = new Subject<Date>();
类别n:

svc.obs.next(new Date());
订户:

svc.obs.subscribe((date) => {
        console.log("Triggered from: " + svc.obs.getSource().classname); // Desired output: "Triggered from: SomeNamespace.Classname"
});

您可以替换
obs:Subject=newsubject()
通过
obs:Subject=newsubject()

然后您可以自己发射源

svc.obs.next({ date: new Date(), source: 'whatever source' });
最后,关于订阅:

svc.obs.subscribe((data) => {
        console.log("Triggered from: " + data.source + "Date is : "+data.date);
});

您可以替换
obs:Subject=newsubject()
通过
obs:Subject=newsubject()

然后您可以自己发射源

svc.obs.next({ date: new Date(), source: 'whatever source' });
最后,关于订阅:

svc.obs.subscribe((data) => {
        console.log("Triggered from: " + data.source + "Date is : "+data.date);
});

解决这个问题的一个干净的办法就是这样做

在你的服务里面

// First, declare an interface like so.
interface ReactiveDate {
    date: Date,
    type: String
}

// Instantiate the Subject using this interface as the generic type.
obs: Subject<ReactiveDate> = new Subject<ReactiveDate>();
// Apply correct logic on "type" inside the subscribe callback.
obs.subscribe(({ date, type }) => {
        console.log("Triggered from: " + type);
});
然后,再次在您的服务中

// First, declare an interface like so.
interface ReactiveDate {
    date: Date,
    type: String
}

// Instantiate the Subject using this interface as the generic type.
obs: Subject<ReactiveDate> = new Subject<ReactiveDate>();
// Apply correct logic on "type" inside the subscribe callback.
obs.subscribe(({ date, type }) => {
        console.log("Triggered from: " + type);
});

因此,“类型”由您自己选择,并且将非常健壮且无错误,因为您总是事先知道值。您将不会通过黑客攻击来解决此问题。

解决此问题的干净方法是这样做

在你的服务里面

// First, declare an interface like so.
interface ReactiveDate {
    date: Date,
    type: String
}

// Instantiate the Subject using this interface as the generic type.
obs: Subject<ReactiveDate> = new Subject<ReactiveDate>();
// Apply correct logic on "type" inside the subscribe callback.
obs.subscribe(({ date, type }) => {
        console.log("Triggered from: " + type);
});
然后,再次在您的服务中

// First, declare an interface like so.
interface ReactiveDate {
    date: Date,
    type: String
}

// Instantiate the Subject using this interface as the generic type.
obs: Subject<ReactiveDate> = new Subject<ReactiveDate>();
// Apply correct logic on "type" inside the subscribe callback.
obs.subscribe(({ date, type }) => {
        console.log("Triggered from: " + type);
});

因此,“类型”由您自己选择,并且将非常健壮且无错误,因为您总是事先知道值。您将不会进行黑客攻击。

请您向我们展示
控制台。在
订阅
中记录
消息,以及调用
obs.next(new Date())
的代码中的实例。
.getSource()
无法输出任何内容,因为此函数不存在。这只是为了证明我正在努力实现的目标。对不起。我似乎误解了这个问题。请稍后提供答案。请向我们展示
控制台中的
日志
消息
订阅
,以及调用
obs.next(new Date())
的代码中的实例。
.getSource()
无法输出任何内容,因为此函数不存在。这只是为了证明我正在努力实现的目标。对不起。我似乎误解了这个问题。很快给出答案。