• eggjs controller层调用controller层解决方案


    序:

            博文原创,转载附上本博文链接即可。

            博主也很无奈,controller里,你login登录调用user权限里的方法肯定是无法避免的,你不可能都放一个类里,况且也没有多继承这种东西,也不适合你放extend/写扩展,不然你到时候挂载app,或ctx就太多了,想想,还是得controller层 2个js文件里的方法得相互可以引用才可以。

            看来大佬都喜欢吃独食啊,百度个毛线没有,没事,关注博主吧,不是什么大佬,但是毕竟13年工作经验,摆在那。

    本博文最终要实现的目标是:controller/a.js 可以调用controller/b.js的方法

    好了,开始正题,说下我的解决方案:

     

    一、目录结构

            2个js:  a.js和b.js ,和路由router.js

    路由router.js加入

    router.get('/api/aaa', controller.a.test1); 

    a.js

    1. const { Controller } = require('egg');
    2. const bb= require('./b');
    3. /**
    4. * @Controller 登录模块
    5. */
    6. class UserController extends Controller {
    7. constructor(m){//这里相当于this了,亲,去看看es6构造函数那一章
    8. super(m)
    9. this.b=new bb(m)
    10. }
    11. /**
    12. * @summary 验证码
    13. * @description 测试swagger文档是否可用
    14. * @router get /api/verify
    15. */
    16. async test1() {
    17. let m=this,{app}=m;
    18. debugger
    19. m.b.conss();
    20. }
    21. }
    22. module.exports = UserController;

      b.js

    1. const { Controller } = require('egg');
    2. /**
    3. * @Controller 登录模块
    4. */
    5. class UserController extends Controller {
    6. constructor(m){
    7. super(m)
    8. }
    9. /**
    10. * @summary 验证码
    11. * @description 测试swagger文档是否可用
    12. * @router get /api/verify
    13. */
    14. async conss() {
    15. let m=this,{app}=m;
    16. console.log(1)
    17. debugger
    18. }
    19. }
    20. module.exports = UserController;

    接口访问

    http://localhost:8033/api/aaa?id=1

    博主原创,转载附上本博文的链接。

         

  • 相关阅读:
    Aptos 生态 18 个精选项目最新梳理(附交互策略)
    Android Studio(数据存储)
    场景应用:图解扫码登录流程
    ansible问题排查
    教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
    企业网络流量分析监控软件
    JVM内存模型之深究模型特征
    【LeetCode每日一题】——404.左叶子之和
    网络安全(黑客)自学
    vue2以ElementUI为例构建notify便捷精美提示
  • 原文地址:https://blog.csdn.net/xuelang532777032/article/details/126098265