当前位置:网站首页>eggjs controller层调用controller层解决方案
eggjs controller层调用controller层解决方案
2022-08-02 05:08:00 【雪狼之夜】
序:
博文原创,转载附上本博文链接即可。
博主也很无奈,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
const { Controller } = require('egg');
const bb= require('./b');
/**
* @Controller 登录模块
*/
class UserController extends Controller {
constructor(m){//这里相当于this了,亲,去看看es6构造函数那一章
super(m)
this.b=new bb(m)
}
/**
* @summary 验证码
* @description 测试swagger文档是否可用
* @router get /api/verify
*/
async test1() {
let m=this,{app}=m;
debugger
m.b.conss();
}
}
module.exports = UserController;
b.js
const { Controller } = require('egg');
/**
* @Controller 登录模块
*/
class UserController extends Controller {
constructor(m){
super(m)
}
/**
* @summary 验证码
* @description 测试swagger文档是否可用
* @router get /api/verify
*/
async conss() {
let m=this,{app}=m;
console.log(1)
debugger
}
}
module.exports = UserController;
接口访问
http://localhost:8033/api/aaa?id=1
博主原创,转载附上本博文的链接。
边栏推荐
- mysql实现按照自定义(指定顺序)排序
- 12个MySQL慢查询的原因分析
- 2022年100道最新软件测试面试题,常见面试题及答案汇总
- 软件测试在职2年跳槽4次,你还在怪老板不给你涨薪?
- Redis-集群模式(主从复制模式,哨兵模式,集群化模式)
- 5年在职经验之谈:2年功能测试、3年自动化测试,从入门到不可自拔...
- Google 安装印象笔记剪藏插件
- 12 reasons for MySQL slow query
- Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
- Timing task library in the language use Cron, rounding
猜你喜欢
MySQL 8.0.29 set and modify the default password
21 Day Learning Challenge Schedule
51单片机外设篇:红外通信
说好的女程序员做测试有优势?面试十几家,被面试官虐哭~~
Go语言之interface详解
navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
为什么4个字节的float要比8个字节的long大呢?
Navicat如何连接MySQL
AMQP协议详解
MySQL multi-table association one-to-many query to get the latest data
随机推荐
非关系型数据库MongoDB的特点及安装
2022年100道最新软件测试面试题,常见面试题及答案汇总
18 years of programmer career, read more than 200 programming books, pick out some essence to share with you
Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)
MySQL夺命10问,你能坚持到第几问?
MySQL 5.7详细下载安装配置教程
Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)
ELK log analysis system
Detailed explanation of mysql stored procedure
mysql 8.0.28版本安装配置方法图文教程
Detailed explanation of interface in Go language
golang's time package: methods for time interval formatting and output of timestamp formats such as seconds, milliseconds, and nanoseconds
MySQL 5.7 detailed download, installation and configuration tutorial
MySQL大批量造数据
Navicat报错:1045-Access denied for user [email protected](using passwordYES)
C language: Check for omissions and fill in vacancies (3)
Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
The company does not pay attention to software testing, and the new Ali P8 has written a test case writing specification for us
2021年软件测试面试题大全
高防服务器防御的原理是什么