[使用Nodejs,我正在调用一个名为customFunction
的函数,该函数是GrpahQL解析器的一个变体。我没有访问this
的权限。
import { Mutation } from './mutation/Mutation'
export default {
Query,
Mutation,
}
然后输入Mutation.ts
import { customFunction } from './customFunction'
export const Mutation = {
customFunction: customFunction.customFunction,
}
然后输入customFunction.ts
export const customFunction = {
test() {
console.log('test called')
},
async customFunction(parent: any, args: any, ctx: any, info: any) {
console.log('customFunction init')
console.log('this', this)
this.test()
console.log('customFunction end')
},
}
这是undefined
,我无法调用同一对象中的函数test()
您在执行此操作时将方法与具有test
方法的对象分开:
import { customFunction } from './customFunction'
相反,您需要做:
import myModule from './customFunction'
然后,您将这样称呼它:
myModule.customFunction()
然后,this
内的customFunction()
值将指向正确的对象,然后this.test()
将起作用。
尝试:
export const customFunction = {
test() {
console.log('test called')
},
async customFunction(parent: any, args: any, ctx: any, info: any) {
console.log('customFunction init')
console.log('this', this)
test()
console.log('customFunction end')
},
}