Adonis js控制器之间如何导出和导入功能

How Export and Import Function between Controllers Adonis js

我有控制器一号

'use strict'

class Controller1 {
    async sum() {
        let a = 10
        let b = 2

        return a * b
    }
}

module.exports = Controller1

在第二个控制器中我有这个

'use strict'
const Controller1 = use('App/Controllers/Http/Controller1')

class Controller2 {
   async othersum() {
       const sum = Controller1.sum()

       return sum + 50
    }
}

module.exports = Controller2

其他函数的调用情况如何

Controller1.sum() 不是静态方法, 您需要创建 Controller1 的实例才能使用 sum() 方法

'use strict'
const Controller1 = use('App/Controllers/Http/Controller1')

class Controller2 {
   async othersum() {
       const ctrl = new Controller1()
       const sum = ctrl.sum()

       return sum + 50
    }
}

module.exports = Controller2