~~~
//service/news.js
'use strict';
const Service = require('egg').Service;
class NewsService extends Service {
async getNewsList() {
//获取新闻数据
var list = ['HTML','css','Javascript'];
return list
}
}
module.exports = NewsService;
~~~
## this.service.news.getNewsList()方法调用数据
~~~
//controller/home.js中使用
'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
/* 调用服务里的方法 */
var list = await this.service.news.getNewsList();
await this.ctx.render('index',{data:"首页的世界",list})
}
async detail(){
this.ctx.body = "detail"
}
}
module.exports = HomeController;
~~~