• 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

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

         

  • 相关阅读:
    产品发布新闻稿件怎么写?稿件撰写技巧分享
    【软件STM32cubeIDE下STM32H743xx使用:看门狗iwdg+复位标志位-基础样例】
    pytorch学习笔记——timm库
    js比较时间戳是否为同一天
    win10配置单一python版本的sublime运行环境
    redis缓存击穿、穿透、雪崩
    JVM报错GC overhead limit exceeded
    RecursionError: maximum recursion depth exceeded情况解决之一
    (利用IDEA+Maven)定制属于自己的jar包
    Java缓存理解
  • 原文地址:https://blog.csdn.net/xuelang532777032/article/details/126098265