当前位置:网站首页>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博主原创,转载附上本博文的链接。
边栏推荐
- 51单片机外设篇:ADC
- 面试官:设计“抖音”直播功能测试用例吧
- Android studio连接MySQL并完成简单的登录注册功能
- 浏览器的onload事件
- apisix-入门使用篇
- MySQL 字符串拼接 - 多种字符串拼接实战案例
- MySQL安装教程
- navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
- Go语学习笔记 - 处理超时问题 - Context使用 从零开始Go语言
- Introduction to Grid Layout
猜你喜欢

coredns介绍

ELK日志分析系统
![[PSQL] 窗口函数、GROUPING运算符](/img/95/5c9dc06539330db907d22f84544370.png)
[PSQL] 窗口函数、GROUPING运算符

51单片机外设篇:点阵式LCD

MySQL 多表关联一对多查询实现取最新一条数据

"Digital reconstruction of the system, getting the CEO is the first step"

How much does a test environment cost? Start with cost and efficiency

100 latest software testing interview questions in 2022, summary of common interview questions and answers

MySQL导入sql文件的三种方法

mysql安装教程【安装版】
随机推荐
How much does a test environment cost? Start with cost and efficiency
[PSQL] 函数、谓词、CASE表达式、集合运算
MySQL 8.0.29 设置和修改默认密码
interrupt()、interrupted()和isInterrupted()你真的懂了吗
Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
配合蓝牙打印的encoding-indexes.js文件内容:
网安学习-内网渗透4
测试技术之APP蓝牙连接测试
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
MySQL夺命10问,你能坚持到第几问?
Navicat cannot connect to mysql super detailed processing method
MySQL安装常见报错处理大全
swinIR论文阅读笔记
51单片机外设篇:点阵式LCD
Redis集群模式
MySQL导入sql文件的三种方法
在腾讯做外包测试的那些日子.....
coredns介绍
C语言入门实战(13):十进制数转二进制
18 years of programmer career, read more than 200 programming books, pick out some essence to share with you