Javascript 如何访问函数';JS中另一个文件中的s对象
我有一个箭头函数,可以导出并在另一个文件中使用(我使用的是React,但这并不重要)。在我的函数中,我有一个具有某些属性的对象。但是,我似乎无法访问该对象 我尝试了两种方法: 1.出口中的出口: 我尝试在现有导出中导出,但不起作用:Javascript 如何访问函数';JS中另一个文件中的s对象,javascript,Javascript,我有一个箭头函数,可以导出并在另一个文件中使用(我使用的是React,但这并不重要)。在我的函数中,我有一个具有某些属性的对象。但是,我似乎无法访问该对象 我尝试了两种方法: 1.出口中的出口: 我尝试在现有导出中导出,但不起作用: export const TextComponent = ({ text }) => { export const Props = { text: [text] } return <p>{Props.text
export const TextComponent = ({ text }) => {
export const Props = {
text: [text]
}
return <p>{Props.text}</p>
}
export const TextComponent=({text})=>{
导出常量道具={
正文:[正文]
}
返回{Props.text}
}
2.试图在没有导出的情况下在另一个文件中访问它
比如:
export const TextComponent=({text})=>{
常量道具={
正文:[正文]
}
返回{Props.text}
}
//文件2
文本组件。道具。。。
这些似乎都不起作用,有没有办法从另一个文件中的函数访问此对象 您可以尝试导出对象,然后在函数中为该对象执行属性赋值。大概是这样的:
export const Props = {};
export const TextComponent = ({ text }) => {
Props.text = [text]
return <p>{Props.text}</p>
}
export const Props={};
导出常量TextComponent=({text})=>{
Props.text=[text]
返回{Props.text}
}
您可以在JS中这样做,但是,如果您在React环境中工作,则不应该以这种方式使用道具
export const Props = {};
export const TextComponent = ({ text }) => {
Props.text = [text]
return <p>{Props.text}</p>
}