当前位置:网站首页>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
博主原创,转载附上本博文的链接。
边栏推荐
猜你喜欢
The Go language learning notes - dealing with timeout - use the language from scratch from Context
Redis-----非关系数据库
Navicat new database
Detailed explanation of interface in Go language
51单片机外设篇:DS18B20
MySql copies data from one table to another table
Matlab论文插图绘制模板第41期—气泡图(bubblechart)
mysql 存储过程详解
Detailed explanation of mysql stored procedure
[email protected](使用passwordYES)"/>
Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)
随机推荐
【C语言】LeetCode26.删除有序数组中的重复项&&LeetCode88.合并两个有序数组
Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
golang泛型
去字节跳动自动化测试二面原题(根据录音整理)真实有效 26
对node工程进行压力测试与性能分析
整合ssm(一)
MySQL 8.0.29 解压版安装教程(亲测有效)
golang的time包:时间间隔格式化和秒、毫秒、纳秒等时间戳格式输出的方法
MySql字符串拆分实现split功能(字段分割转列、转行)
关于web应用的目录结构
利用浏览器本地存储 实现记住用户名的功能
[PSQL] window function, GROUPING operator
go项目的打包部署
公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范
Introduction and use of apifox (1).
Go language study notes - grpc serverclient protobuf Go language from scratch
Mysql存储json格式数据
Detailed explanation of AMQP protocol
H5接入支付流程-微信支付&支付宝支付
构造方法、成员变量、局部变量