【问题标题】:How to access the method defined in one class to another class in TypeScript如何将一个类中定义的方法访问到 TypeScript 中的另一个类
【发布时间】:2022-11-06 04:07:47
【问题描述】:

我已经导出了其中定义了handle() 方法的类,我只想在另一个类中访问这个方法。下面是我的代码:

客户.push.handler.ts

import { Inject } from "@nestjs/common";
import { EventsHandler, IEventHandler } from "@nestjs/cqrs";
import { CustomerPushEvent } from "../events/customer.push.events";
const admin = require('firebase-admin');

@EventsHandler(CustomerPushEvent)
export class CustomerPushEventHandler implements IEventHandler<CustomerPushEvent>{
  constructor(
     @Inject('Logger') private readonly log,
  ) {}

async handle(customerEvent:CustomerPushEvent){

    try{

        const message = {
            topic: String(customerEvent.customerId),
            notification: {
                title:'Dummy notification',
                body: 'New Shipment !!',
            },
            android: {
                collapse_key: 'messages',
                priority: 'HIGH',
            },
        };
      
        // Send a message to the device corresponding to the provided
        // registration token.
        admin
            .messaging()
            .send(message)
            .then(response => {
                console.log('Truck has been assigned:', message);
                console.log('Customer event',customerEvent);
            })
            .catch(error => {
                console.log('Error sending message:', error);
            });
    }
    catch(err){
        this.log.error(
            { error: err && err.message },
            'Error while communicating with FCM',
        );
    }
  }
}

另一个类.ts

import { Inject } from "@nestjs/common";
import { ChatService } from "src/chats/chat.service";
import * as _ from 'lodash';
import { CustomerPushEventHandler } from "src/chats/event-handlers/customer.push.handler";

 export class CustomerNotificationEventhandler {
   constructor(@Inject('Logger') private readonly log,
    private readonly chatService: ChatService,
    @Inject('CustomerNotificationConsumer') private readonly customerNotifConsumer,
   ) {
       this.handle();
    }

   async handle() {
        
      //Here I awant to access handle() method defined in CustomerPushEventHandler class 
   }
}     

我怎样才能做到这一点?

【问题讨论】:

  • 使CustomerPushEventHandler 可注入,并将此类添加到提供程序数组中。
  • 你能显示代码吗?

标签: typescript nestjs


【解决方案1】:

您应该将 CustomerPushEventHandler 添加到 CustomerNotificationEventhandler 构造函数中。所以代码应该是这样的:

import { Inject } from "@nestjs/common";
import { ChatService } from "src/chats/chat.service";
import * as _ from 'lodash';
import { CustomerPushEventHandler } from "src/chats/event-handlers/customer.push.handler";

 export class CustomerNotificationEventhandler {
   constructor(@Inject('Logger') private readonly log,
    private readonly chatService: ChatService,
    @Inject('CustomerNotificationConsumer') private readonly customerNotifConsumer,
    private readonly customerPushEventHandler: CustomerPushEventHandler
   ) {
       this.handle();
    }

   async handle() {
        this.customerPushEventHandler.handle()
      //Here I awant to access handle() method defined in CustomerPushEventHandler class 
   }
} 

【讨论】:

    【解决方案2】:

    首先,使CustomerPushEventHandler可注入

    @Injectable()
    export class CustomerPushEventHandler implements IEventHandler<CustomerPushEvent>{
        // class properties & methods
    }
    

    然后在模块的提供者数组中添加CustomerPushEventHandler

    @Module({
        providers: [
            CustomerPushEventHandler
        ],
    })
    export class AppModule {
    }
    

    然后注入CustomerNotificationEventhandler

    export class CustomerNotificationEventhandler {
        constructor( private readonly customerPushEventHandler: CustomerPushEventHandler
        ) {
        this.handle();
        }
    
        async handle() {
            // you need CustomerPushEvent object here to proceed further
            this.customerPushEventHandler.handle(customerEvent)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-03
      • 2018-10-18
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      相关资源
      最近更新 更多