【发布时间】:2017-12-16 17:56:34
【问题描述】:
我需要获取导入类的文件名:
文件A.js
export default class User {
}
fileB.js
import User from './fileA'
function getClassFilename(constructor) {
// do something like __filename, but to get the filename where User is defined rather than the current filename
}
console.log(getClassFilename(User.constructor)) // fileA.js
这是一般的想法。然而实际用例是基于装饰器的:
文件A.js
import someDecorator from './decorator'
@someDecorator
class User {
}
装饰器.js
export default function (target) {
// can I somehow get the target filename without passing it as a property?
}
【问题讨论】:
标签: javascript filenames