如何导入本机其他文件夹中的图像?

问题描述 投票:0回答:1

我的应用程序的项目结构是:-

root
|-assets
    |-icons
       |-heart.png

|-components
    |-basic
        |-like.js

我想要实现的是将图像heart.png导入到like.js,其中包含以下代码:-

import React,{ Component } from 'react';
import{ View, Text, AppRegistry } from 'react-native';
class Example extends Component {
  render ()
   {
    return(
    <Image src= {require('./assets/icons/heart.png')} />    
    );
 }}

export default Example;

它产生以下错误:-

ENOENT: no such file or directory, scandir 'C:\Users\gagan\sapora\android\sapora\components\basic\assets\icons'
javascript react-native components assets
1个回答
0
投票

使用source代替使用src

render ()
   {
    return(
    <Image source = {require('../assets/icons/heart.png')} />    
    );
 }

请参见https://facebook.github.io/react-native/docs/image

© www.soinside.com 2019 - 2024. All rights reserved.