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

时间:2019-03-21 14:18:02

标签: node.js controller 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

如何在其他函数中调用

1 个答案:

答案 0 :(得分:2)

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