将变量从一个方法传递到另一个文件中的另一个方法

时间:2019-07-12 08:13:40

标签: typescript

在文件components.ts中,我在方法checkVersionFile()内有一个变量newVersion。 现在在另一个文件(version.ts)中,我想在另一个方法中

检索此变量。

文件:components.ts

public checkVersionFile(): any {
        const contentVersion = fs.readFileSync(this.tmpDir + "/components/Version", 'utf8')
        if(contentVersion.endsWith('-SNAPSHOT')){
            let match = /^(\d+)\.(\d+)\.(\d+)-(\d+)-(SNAPSHOT)$/.exec(contentVersion);
            const newVersion =  (parseInt(match[1])) + '.' + (parseInt(match[2])) + '.' + (parseInt(match[3])) + '-' + (parseInt(match[4]));
            logger.info("Version: "+newVersion);      
        }
        else {
            logger.error("error"); 
        }
    }

文件version.ts

public async retrieveVersion(): {
        let searchVersionsUri = config.jiraApiUri + 'project/versions';       
        const jsonResp = await this.jiraClient.get(searchVersionsUri);
        const version: any = jsonResp.find(version => {
            return version.name == newVersion  // here want retrieve variable

        });
        return new ReleaseVersion(version.id, version.name, version.released);
    }

2 个答案:

答案 0 :(得分:0)

这实际上取决于您的编译设置。如果将所有.ts文件编译为一个.js文件,则该文件应该已经可以使用。还是要使用模块?

TSCONFIG.JSON

{
    "compilerOptions": {
        "outFile": "docs/js/main.js"
    },
    "include": [
        "dev/**/*"
    ]

}

答案 1 :(得分:0)

我认为您不能直接 export在函数内部的变量。相反,您可以使函数checkVersionFile返回newVersion的值和export方法,然后返回import。在version.ts中,使用checkVersionFile()可以得到newVersion的值。 希望对您有所帮助。

相关问题