简体   繁体   English

如何使用参数订阅可观察函数的主题

[英]How to subscribe subject to observable function with params

I have function with arbitrary params of observable that is doing some job.我有可以做一些工作的带有任意参数的 observable 函数。 I want to subscribe to it a subject that have other some subscription (for make all triggered for the functions called).我想订阅一个主题,它有其他一些订阅(对于调用的函数触发所有)。

I can't find the way to do it right.我找不到正确的方法。

My service :我的服务:

//The method of the observable 
addProduct2(product: Product) {     
  delete product.id;
  const url = 'http://localhost:8080/createProduct';
  return this.http.post<Product>(url, product);
}

I want that this will work :我希望这会起作用:

getProductsEvent:Subject<Product> = new Subject();

constructor(private http: HttpClient, private currentRoute: ActivatedRoute) {
  //....
  this.addProduct2.subscribe(this.getProductsEvent);
}

I am getting this compiling error :我收到此编译错误:

ERROR in src/app/components/products-feature/data-service.service.ts(42,22): error TS2339: Property 'subscribe' does not exist on type '(product: Product) => Observable'. src/app/components/products-feature/data-service.service.ts(42,22) 中的错误:错误 TS2339:属性 'subscribe' 不存在于类型 '(product: Product) => Observable'。

and in the logs :并在日志中:

AppComponent.html:3 ERROR TypeError: this.addProduct2.subscribe is not a function at new DataServiceService (data-service.service.ts:42)... AppComponent.html:3 ERROR TypeError: this.addProduct2.subscribe 不是新 DataServiceService (data-service.service.ts:42) 的函数...

How can I achieve it right?我怎样才能正确地实现它? What's I'm missing?我缺少什么?

remember that I can't invoke记住我不能调用

this.addProduct2().subscribe...; 

because it's have a params, and I don't know what params it will get.因为它有一个参数,我不知道它会得到什么参数。

If you want to emit an event every time a new product is created you have to call next on your productsEvent Subject .如果您想在每次创建新产品时发出一个事件,您必须在 productsEvent Subject上调用 next 。

private productEvent: Subject<Product> = new Subject(); 

public getProductsEvent(); Subject<Product> {
  return this.productEvent;
}

public addProduct(product: Product): Observable<Product> {
  delete product.id;
  const url = 'http://localhost:8080/createProduct';
  return this.http.post<Product>(url, product).pipe(
    tap((product: Product) => this.productEvent.next(product)),
  );
}

I used a pipe and the pipable tap operator to call next after the new product is returned from your http post request.在从您的 http post 请求返回新产品后,我使用了管道和可管道的 tap 运算符来调用 next 。 The addProduct method returns an observable that means nothing will happen until you subscribe to it. addProduct方法返回一个 observable,这意味着在您订阅它之前什么都不会发生。

With everything setup like this you can use it as follows:通过这样的所有设置,您可以按如下方式使用它:

Somewhere you can add a product by calling the method and subscribing.您可以在某个地方通过调用方法和订阅来添加产品。

addProduct(data).subscribe();

And anywhere where you subscribed to the event you will be notified:在您订阅事件的任何地方,您都会收到通知:

getProductsEvent().subscribe((addedProduct: Product) => {
  // Do something with the added product
  console.log(addedProduct)
});

Note I kept the types like in your question, but I wonder if the type of the data you post Product is the same as the type of the response, also Product but I guess you know your object types best, so I left it as is.注意我保留了您问题中的类型,但我想知道您发布Product的数据类型是否与响应类型相同,还有Product但我想您最了解您的对象类型,所以我保持原样.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM