Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
No se puede llamar a un método declarado en una clase desde otra clase singleton

Como dice el título, estoy tratando de acceder a un método que está declarado en una clase (Publicación) desde otra clase (Comentarios) que sigue un patrón único. La clase de publicación es una clase de servicio que tiene algunos métodos para realizar llamadas a la API. Así que necesito tener acceso a ellos desde dentro de la clase Comentarios para poder hacer llamadas a la API.

Así es como se ve una versión simplificada de la clase Post en este momento:

 @Injectable({ providedIn: 'root' }) class PostService extends AnotherService { constructor( auth: AuthService, http: HttpClient ) { super('string', auth, http); } getPost( id: string ) { return this.http.get(`posts/${id}`); } }

Así es como se ve la clase Comentarios:

 class Comments { private postService: PostService; private static instance; private constructor() {} static createInstance() { if ( !Comments.instance ) { Comments.instance = new Comments(); } return Comments.instance; } getComments( id ) { // these does not even run this.postService.getPost( id ) .then( post => { console.log( post ); }) .catch( error => { console.log( error ); }); } }

¿Cómo puedo hacer para acceder a él?

=========ACTUALIZAR=======

Creando una instancia de la clase Comment en otra clase llamada ClassC.

 const instance - Comments.createInstance(); instance.getComments( id );
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use un nuevo servicio para guardar los datos de su objeto de comentario, digamos que tenemos un servicio llamado SharedDataService.

 private _comments: Array<any> =[];// or array<IComment> (a defined interface from your part) class SharedDataService(){} get comments():Array<any>{ return this._comments} set comments(value:Array<any>){ this._comments = value; } }

Debe iniciar PostService en su constructor de comentarios

 private constructor(private postService: PostService,private sharedDataService :SharedDataService) { } getComments() { // these does not even run this.postService.getPost( '1' ) .then( post => { this.sharedDataService.comments = post // if You get an array of comments here console.log( post ); console.log(this.comments)// getter function its new value has been set }) .catch( error => { console.log( error ); }); get comments(){ this.sharedDataService.comments } }
about 4 years ago · Juan Pablo Isaza Report

0

Si desea enviar dos solicitudes http en paralelo, obtenga sus valores. Debe usar el operador combineLatest rxjs. Su servicio de correos será así:

 getPost(id: string) { $postDataHttpRequest = this.http.get(`posts/${id}`); $commentsDataHttpRequest = this.http.get(`posts/${id}/comments`); return combineLatest($postDataHttpRequest, $commentsDataHttpRequest) }

///

Así es como se ve la clase Comentarios:

 private constructor(private postService: PostService) { } getComments() { this.postService.getPost( '1' ) .subscribe( (posts,comments) => { console.log(posts); console.log(comments); },(error)=>{console.log(error)}) }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!