当前位置:网站首页>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博主原创,转载附上本博文的链接。
边栏推荐
猜你喜欢

nacos注册中心

MySQL 8.0.29 解压版安装教程(亲测有效)
[email protected](using passwordYES)"/>Navicat报错:1045-Access denied for user [email protected](using passwordYES)
![[PSQL] window function, GROUPING operator](/img/95/5c9dc06539330db907d22f84544370.png)
[PSQL] window function, GROUPING operator

mysql 8.0.28版本安装配置方法图文教程

Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)

ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution

面试官:设计“抖音”直播功能测试用例吧

MySQL 8.0.28 version installation and configuration method graphic tutorial

Use the browser's local storage to realize the function of remembering the user name
随机推荐
去字节跳动自动化测试二面原题(根据录音整理)真实有效 26
CPU使用率和负载区别及分析
navicat无法连接mysql超详细处理方法
go项目的打包部署
PIL与numpy格式之间的转换
Google 安装印象笔记剪藏插件
Detailed installation and configuration of golang environment
swinIR论文阅读笔记
说好的女程序员做测试有优势?面试十几家,被面试官虐哭~~
The original question on the two sides of the automatic test of the byte beating (arranged according to the recording) is real and effective 26
Mysql子查询关键字的使用(exists)
golang的time包:时间间隔格式化和秒、毫秒、纳秒等时间戳格式输出的方法
MySQL如何创建用户
165.比较版本号
MySQL 多表关联一对多查询实现取最新一条数据
TikTok平台的两种账户有什么区别?
Navicat new database
JDBC revisited
复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式
Use the browser's local storage to realize the function of remembering the user name