【发布时间】:2020-04-27 16:34:51
【问题描述】:
我正在尝试从我的项目中的本地文件夹动态渲染图像
对象结构是这样的
{
id: 2,
text: 'How would you describe your level of influence in management of the farm?',
type: 'cardChoice',
choices: [
{
id: 1,
text: 'I am the primary decision maker',
questionId: 'Hm?',
value: 'I am the primary decision maker',
image: '1.jpg',
},
{
id: 2,
text: 'I hent',
questionId: 'Hrm?',
value: 'I',
image: '2.jpg',
},
{
id: 3,
text: 'I arm',
questionId: '?',
value: 'Irm',
image: '3.jpg',
},
],
},
在我的组件中,我选择包含图像const baseUrl = '../../assets/images/CNA'
的文件夹
之后,在我返回时,我尝试渲染图像
<img src={`${baseUrl}'${questionChoice.image}'`} alt={questionChoice.text} />
页面呈现,但我的图片没有加载,而是显示我的alt
这是我的完整组件
const CardChoiceQuestions = ({ cardChoiceArray, currentAnswer, updateCurrent, submitAnswer }) => {
const { id, value } = currentAnswer
const baseUrl = '../../assets/images/CNA'
return (
<ButtonContainer>
{cardChoiceArray.map(questionChoice => {
return (
<Button
active={questionChoice.id === id}
type="button"
key={questionChoice.id}
onClick={() => {
const answer = { id: questionChoice.id, value: questionChoice.value }
updateCurrent(answer)
submitAnswer()
}}
>
<p>{questionChoice.text}</p>
<img src={`${baseUrl}${questionChoice.image}`} alt={questionChoice.text} />
</Button>
)
})}
</ButtonContainer>
)
}
【问题讨论】:
标签: reactjs image oop ecmascript-6