我们如何在 es6 类中访问 koa js 路由context
(this)和 es6this
类?
当我尝试使用this.name
我未定义访问类属性时
测试.js
export default class {
constructor (opts) {
this.name = 'test'
}
welcome () {
console.log(this.name) // 'this' is UNDEFINED. Trying to access class property. But getting undefined
this.body = 'welcome '+this.params.name // 'this' works fine here as a koa reference
}
}
应用程序.js
import test from './test'
let Test = new test({})
router.get('/test/:name', Test.welcome)