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

138
Views
Las propiedades no están definidas dentro de un decorador.

Estoy tratando de llamar a una función que tiene un decorador adjunto. Lo que hace es ejecutarse cada x segundos, lo que funciona bien; sin embargo, cuando intento registrar las propiedades en la función, generan undefined , pero en el constructor muestra la clase adecuada.

Si llamo a otra función que está en la clase, se llama a la función, el problema parece ser al acceder a las propiedades.

Estas son algunas de las formas en que he intentado llamar al método:

 export function Repeat(interval: number, delay: number = 0) { return (target: any, prop: any, descriptor: PropertyDescriptor) => { timer(delay * 1000, interval * 1000) .pipe( tap(i => descriptor.value.call(target, i)), tap(i => target[prop].call(target, i)), tap(i => target[prop].bind(target)(i)) ) .subscribe(); }; }

Así es como estoy implementando el decorador:

 export class MyClass { constructor(private readonly v: Item) { console.log(this.v); // outputs: typeof Item } @Repeat(1) repeat(count: number) { console.log(this.v); // outputs: undefined this.testFunc(); // runs the function } testFunc(){ console.log('test function'); } }

Aquí hay un ejemplo de stackblitz

Editar

Después de jugar un poco, puedo ver que puedo hacer esto:

 export function Repeat(interval: number, delay = 0) { return function (target: any, prop: string, descriptor: PropertyDescriptor) { const originalMethod = descriptor.value; descriptor.value = function (this: GameObject, ...args: any[]) { const t = timer(delay * 1000, interval * 1000) .pipe(map<number, boolean>(i => originalMethod.apply(this, [i]))) .subscribe(); }; return descriptor; }; }

Luego, cuando lo llamo en la clase, el repetidor funciona:

 export class MyClass { constructor(private readonly v: Item) { this.repeat(0); } @Repeat(1) repeat(count: number) { console.log(this.v); // outputs: typeof Item } }

Sin embargo, esto no es a lo que quiero recurrir, me gustaría que la función se llamara automáticamente y no manualmente.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Entonces, para resolver mi problema, tuve que usar un decorador en el nivel de clase junto con el nivel de método. Luego, en el constructor, ejecuto el método en la clase principal.

Primero importa el módulo reflect-metadata :

 import 'reflect-metadata';
 export function Component() { return <T extends { new (...args: any[]): any }>(target: T): any => { return class extends target { constructor(...args: any[]) { super(...args); const methods = Reflect.ownKeys(target.prototype) as string[]; methods.forEach((method) => { // Get the saved metadata const r = Reflect.getMetadata('token', target.prototype, method); if (typeof r === 'function') { // Here is where we call the method. this[method](); } }); } }; }; }

Aquí está la función de repetición:

 export function Repeat(interval: number, delay = 0, times = Infinity) { return function (target: any, prop: string, descriptor: PropertyDescriptor) { // Define the metadata to be used for later Reflect.defineMetadata('token', target[prop], target, prop); const originalMethod = descriptor.value; descriptor.value = function (this: any, ...args: any[]) { timer(delay * 1000, interval * 1000) .pipe(map<number, boolean>((i) => originalMethod.apply(this, [i]))) .subscribe(); }; return descriptor; }; }

Así es como se implementa en la clase:

 @Component() export class MyClass { constructor(private readonly v: Item) { console.log(this.v); // outputs: typeof Item } @Repeat(1) repeat(count: number) { console.log(this.v); // outputs: typeof Item } }

Aquí está el ejemplo de stackblitz .

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!