文件中的多个组件 [英] Multiple components within a file
问题描述
说我有成分A
喜欢
export default class ComponentA extends components {
render(){
return() //use componentB here?
}
}
class ComponentB extends components {
}
如何在ComponentA中创建另一个组件并使用它?
how can I create another component and use it within ComponentA?
推荐答案
如何在ComponentA中创建另一个组件并使用它?
How can I create another component and use it within ComponentA?
有两种可能的方法:
1 - 在同一文件中定义组件,不需要导出该组件,因为您将在同一文件中使用该组件。
1- Define the component in the same file, exporting of that component will be not required because you will use that component in the same file.
2 - 在另一个文件中定义组件,然后导出该组件。在这种情况下,将需要导入组件。
2- Define the component in another file then export that component. Importing of component will be required in this case.
我们可以在同一个文件中创建任意数量的组件,我们可以像使用HTML标签一样使用这些组件 div,span,p
等。
We can create as many components as we want in the same file, and we can use those components in the same way as we use HTML tags div, span, p
etc.
示例:
在另一个组件中使用 ComponentB
ComponentA
:
Using ComponentB
inside another component ComponentA
:
export default class ComponentA extends components {
render(){
return(
<div>
{/*other code*/}
<ComponentB /> // notice here, rendering ComponentB
</div>
)
}
}
定义 ComponentB
在同一个文件中:
class ComponentB extends components {
}
在另一个文件中定义 ComponentB
:
Define ComponentB
like this in another file:
export default class ComponentB extends components {
}
这篇关于文件中的多个组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!