gpt4 book ai didi

typescript - 导出类的公共(public)属性具有或正在使用私有(private)名称

转载 作者:行者123 更新时间:2023-12-05 07:50:18 25 4
gpt4 key购买 nike

我在看这个question但是导出并不能解决我的问题。

我有以下代码片段,cloudFoundry.ts:

export var cf = require.__$__nodeRequire<any>('cf-client');

export class CloudFoundry {

apps: typeof cf.Apps;
cloudController: typeof cf.CloudController;
domains: typeof cf.Domains;
logs: typeof cf.Logs;
usersUAA: typeof cf.UsersUAA;
routes: typeof cf.Routes;
organizations: typeof cf.Organizations;
spaces: typeof cf.Spaces;
services: typeof cf.Services;

constructor() {
// load services with proper token + config
this.initializeServices();
return;
}

....

private initializeServices() {
['cloudController', 'apps', 'domains', 'organizations', 'routes', 'spaces', 'services', 'logs','usersUAA'].map(function(_class){
eval(`this.${_class} = new(cf).${_class.charAt(0).toUpperCase() + _class.slice(1)}(this._endpoint)`);
eval(`this.${_class}.setToken(this.token)`);
}, this);
this.cloudController.getInfo().then((result) => {
this.usersUAA.setEndPoint(result.authorization_endpoint);
this.logs.setEndPoint(result.logging_endpoint);
}).catch((e) => {
console.log('ERROR ' + e);
return;
});
}
}

然后是另一个文件viewlet.ts

...
import {
CloudFoundry,
OauthToken
} from './cloudFoundry';
...

但是 typescript 抛出以下错误。

cloudFoundry.ts(24,15): Public property 'apps' of exported class has or is using private name 'cf'.
cloudFoundry.ts(25,26): Public property 'cloudController' of exported class has or is using private name 'cf'.
cloudFoundry.ts(26,18): Public property 'domains' of exported class has or is using private name 'cf'.
cloudFoundry.ts(27,15): Public property 'logs' of exported class has or is using private name 'cf'.
cloudFoundry.ts(28,19): Public property 'usersUAA' of exported class has or is using private name 'cf'.
cloudFoundry.ts(29,17): Public property 'routes' of exported class has or is using private name 'cf'.
cloudFoundry.ts(30,24): Public property 'organizations' of exported class has or is using private name 'cf'.
cloudFoundry.ts(31,17): Public property 'spaces' of exported class has or is using private name 'cf'.
cloudFoundry.ts(32,19): Public property 'services' of exported class has or is using private name 'cf'.

我尝试了提到的技巧 here通过导出导入,至少消除了错误。

export var cf = require.__$__nodeRequire<any>('cf-client');

但后来我在运行时遇到了以下问题......

cf is not defined: ReferenceError: cf is not defined
at eval (eval at <anonymous> (cloudFoundry.js:41:108)

该行指的是以下内容。

eval("this." + _class + " = new(cf)." + (_class.charAt(0).toUpperCase() + _class.slice(1)) + "(this._endpoint)"); 

如果您能尝试解决此问题,我们将不胜感激。

最佳答案

我最终通过导出导入解决了这个问题:

export var cf = require.__$__nodeRequire<any>('cf-client');
export class CloudFoundry {
apps: typeof cf.Apps;
...

在使用时也导入库。

private initializeServices() {
var cf = require.__$__nodeRequire<any>('cf-client');
....

关于typescript - 导出类的公共(public)属性具有或正在使用私有(private)名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36308973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com