GithubHelp home page GithubHelp logo

Comments (5)

jacksky007 avatar jacksky007 commented on August 26, 2024

@DragWeb 你的示例代码不全,参照 egg 文档,你的代码应该是这样

const Controller = require('egg').Controller
module.exports = class homeController extends Controller {
  * index() {
    console.log('app=====', app)
    yield this.ctx.render('index/index.js', this.ctx);
  }
};

from egg-vue-webpack-boilerplate.

DragWeb avatar DragWeb commented on August 26, 2024

@jacksky007 thanks!

from egg-vue-webpack-boilerplate.

DragWeb avatar DragWeb commented on August 26, 2024

hi @jacksky007 controller层按下面的写法,那router.js如何写呢?

const Controller = require('egg').Controller
module.exports = class homeController extends Controller {
  * index() {
    yield this.ctx.render('index/index.js', this.ctx);
  }
};

这样写找不到路由,直接404了

const controller = app.controller
app.get('/', controller.home.home.index)

router.js文件中this.ctx是一个空对象,拿不到homeController

我的目的就是想跟controller调用Service中的方法一样(ctx.service.biz.user)ctx.controller.xx.xx

from egg-vue-webpack-boilerplate.

jacksky007 avatar jacksky007 commented on August 26, 2024

@DragWeb 就你贴的内容我也不知道是啥问题,egg 路由文档是这样的

module.exports = app => {
  app.get('/', app.controller.home.home.index)
}

我刚接触egg,还很不熟悉,但router.js中肯定是不能访问context的,context是请求级别的对象,而router.js是整个egg框架启动过程中使用的初始化信息,所以想在router.js用this.context方式肯定是不对的。

from egg-vue-webpack-boilerplate.

DragWeb avatar DragWeb commented on August 26, 2024

@jacksky007 嗯,是的,确实在router.js拿不到context,看来只看按文档方式做了,感谢感谢!
@hubcarl 建议在controller与router可以用class方式做那就更好了

from egg-vue-webpack-boilerplate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.