我有一个类似的代码库
//Cart.ts
import getDataForRequest from '../getData'
class Cart{
private user;
private pwd;
private env; // THIS IS THE MEMBER I WANT TO MAKE AVAILABLE TO OTHER PARTS OF THE APP
constructor(user: string, pwd: string, env: string) {
this.user = user
this.pwd = pwd
this.env = env
}
doSomething() {
getDataForRequest()
}
}
// getData.ts
export getDataForRequest() {
doSomething(env) // <== this has to come from that member in Cart
}
我想让该课程的env
成员可用于应用程序中的其他模块。
因此,当我将类实例化为const foo = new Cart('user123', '12345', 'www.google.com')
时,也可以从getData文件访问最后一个参数,而无需在doSomething()
中传递参数。
答案 0 :(得分:0)
将其作为参数传递。
更改:
doSomething() {
getDataForRequest()
}
收件人:
doSomething() {
getDataForRequest(this.env)
}