Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React(JS)如何获取当前模块导入的文件的文件名?_Javascript_Reactjs - Fatal编程技术网

Javascript React(JS)如何获取当前模块导入的文件的文件名?

Javascript React(JS)如何获取当前模块导入的文件的文件名?,javascript,reactjs,Javascript,Reactjs,我正在编写一个react库(react xarrows.tsx),当用户向我的组件提供错误的属性时,会抛出一个错误(我写的),但它显示了我在“react arrows.tsx”中写的行 出现错误时,用户将看到: /src/Xarrow.tsx:184:12 if (!("current" in props.start)) throw Error( `'start' property is not of type reference. maybe

我正在编写一个react库(react xarrows.tsx),当用户向我的组件提供错误的属性时,会抛出一个错误(我写的),但它显示了我在“react arrows.tsx”中写的行

出现错误时,用户将看到:

/src/Xarrow.tsx:184:12
    if (!("current" in props.start))
      throw Error(
        `'start' property is not of type reference.
        maybe you set 'start' to other object and not to React reference?.\n`
      );
我想向用户指出错误的确切位置-我可以通过向Error提供'filename'-Error([message[,filename[,lineNumber]])-导入当前模块的文件的文件名,使用我的lib的用户文件-让我们称该文件为'/src/Example5.tsx'。 (关于“名称”,我当然是指相对路径)

现在,我需要弄清楚如何获得这个文件名(以及稍后的行号)

(我确实试图从'moudle.parents'对象中获取它,作为其他类似问题的答案,但没有任何模块对象的父属性。 是最接近于帮助我的。)

一定有办法做到这一点,因为react本身向用户指出了他错在哪里。 我在Example5中留下了一个错误示例(只需单击Example5)。您可以自由使用它,如果您提出了解决方案,请与他人分享

简化示例5:

import React,{useState,useRef}来自“React”;
从“./src/Xarrow”导入Xarrow;
const canvasStyle:React.cssprroperties={/*style things*/};
const-boxStyle={/*style-things*/};
常量框:React.FC=props=>{
返回(
{props.box.id}
);
};
常量示例5:React.FC=()=>{
const[box,setBox]=useState({id:“box1”,ref:useRef(null)});
const[box2,setBox2]=useState({id:“box2”,ref:useRef(null)});
返回(
{lines.map((line,i)=>(
//我想在“start={box}”上指出错误,因为“box”不是引用
))}
);
};
导出默认示例5;

这是否回答了您的问题@DanielGeffen不,我在仅跟踪的react开发文件中没有“/src/UsersComponent.tsx”。请不要轻率地提出这个建议来解决我的问题。你自己看看吧-