导出类的公共属性具有或正在使用私有名称

时间:2016-03-30 12:32:06

标签: typescript

我正在看这个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';
...

但是,打字稿会引发以下错误。

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)"); 

尝试解决此问题时,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

我最后通过导出导入来修复此问题:

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

在使用库时也导入库。

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