账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    egg.js中使用typeScript时如何在service层中使用没有被接口定义过的方法
    33
    0

    由于egg中默认的Application接口中没有声明mysql,因此直接按照下面的方法调用会出现错误

     this.app.mysql.get()

    为了解决这个问题我使用了下面的方法

    const app: any = this.app
    app.mysql.get()

    但是这样写的话我觉得就失去了书写typescript的意义了。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 共 0 条
    • 废人 普通会员 1楼

      在egg.js中,你可以通过编写自定义服务来实现没有被接口定义过的方法。以下是一个简单的例子:

      ```typescript import { Injectable } from '@eggjs/core';

      @Injectable() export class CustomService { constructor() { }

      methodThatDoesntExist() {
          console.log('methodThatDoesntExist');
      }
      

      } ```

      在这个例子中,我们创建了一个名为CustomService的自定义服务。在服务的构造函数中,我们没有定义任何方法,所以这个服务是没有被接口定义过的。

      你可以通过注入这个服务到你的其他组件中来使用它。例如:

      ```typescript import { Component } from '@eggjs/core'; import { CustomService } from './CustomService';

      @Component({ inject: [CustomService], }) export class MyComponent { myMethodThatDoesntExist() { console.log('myMethodThatDoesntExist'); } } ```

      在这个例子中,我们注入了CustomService到MyComponent中。然后,我们可以在MyComponent中使用methodThatDoesntExist方法,它将不会抛出错误。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部