当前位置:网站首页>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 5.7 upgrade to 8.0 detailed process
- 18 years of programmer career, read more than 200 programming books, pick out some essence to share with you
- H5 access payment process - WeChat payment & Alipay payment
- 51单片机外设篇:红外通信
- kubernetes 亲和、反亲和、污点、容忍
- LeetCode刷题系列 -- 787. K 站中转内最便宜的航班
- C语言入门实战(13):十进制数转二进制
- apisix-Getting Started
- H5接入支付流程-微信支付&支付宝支付
- Redis-----非关系数据库
猜你喜欢

AMQP协议详解

mysql安装教程【安装版】

ELK log analysis system
[email protected](使用passwordYES)"/>Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)

25K测试老鸟6年经验的面试心得,四种公司、四种问题…

JDBC revisited

Detailed explanation of interface in Go language

C language: Check for omissions and fill in vacancies (3)

Google 安装印象笔记剪藏插件

coredns介绍
随机推荐
面试测试工程师一般会问什么?测试主管告诉你
go语言中的goroutine(协程)
PSQL function, predicate, CASE expression and set operations
整合ssm(一)
el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
构造方法、成员变量、局部变量
公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范
Go语言之interface详解
Android studio连接MySQL并完成简单的登录注册功能
Google notes cut hidden plug-in installation impression
Navicat cannot connect to mysql super detailed processing method
go项目的打包部署
The company does not pay attention to software testing, and the new Ali P8 has written a test case writing specification for us
MySQL导入sql文件的三种方法
本周大新闻|苹果MR已进行Pre-EVT测试,Quest 2涨价100美元
MySQL 8.0.29 set and modify the default password
测试环境要多少?从成本与效率说起
apifox介绍及使用(1)。
Grid布局介绍
Navicat报错:1045-Access denied for user [email protected](using passwordYES)