我在类构造函数中声明了一个属性,并通过使用“ this”声明为“ static”的方法进行访问,并且该方法不可访问。如何获取静态方法中的构造函数(类)变量?
export class Reporter {
constructor() {
this.jsonReports = path.join(process.cwd(), "/reports/json")
this.cucumberReporterOptions = {
jsonFile: targetJson,
output: htmlReports + "/cucumber_reporter.html",
reportSuiteAsScenarios: true,
theme: "bootstrap",
}
}
static createHTMLReport() {
try {
reporter.generate(this.cucumberReporterOptions);
} catch (err) {
}
}
}
更新:
按照“ @CodingIntrigue”,我已经在'reporter.js'文件中做到了这一点,并在我的配置文件中将该方法称为Reporter.createHTMLReport()并按预期工作。但不确定这是否是最佳做法。
const jsonReports = path.join(process.cwd(), "/reports/json")
const cucumberReporterOptions = {
jsonFile: targetJson,
output: htmlReports + "/cucumber_reporter.html",
reportSuiteAsScenarios: true,
theme: "bootstrap",
}
export class Reporter {
static createHTMLReport() {
try {
reporter.generate(cucumberReporterOptions);
} catch (err) {
}
}
}
最佳答案
如果要继续使用class
语法,也可以只设置jsonReports
和cucubmerReporterOptions
静态属性:
export class Reporter {
static createHTMLReport() {
try {
reporter.generate(Reporter.cucumberReporterOptions);
} catch (err) {
}
}
}
Reporter.jsonReports = path.join(process.cwd(), "/reports/json")
Reporter.cucumberReporterOptions = {
jsonFile: targetJson,
output: htmlReports + "/cucumber_reporter.html",
reportSuiteAsScenarios: true,
theme: "bootstrap",
}