【问题标题】:How do I get local image using separate data.jsx file?如何使用单独的 data.jsx 文件获取本地图像?
【发布时间】:2021-07-22 15:43:07
【问题描述】:

两种方法都试过了,还是不行!

const Data = [
    {
        image: ('./cake.png'),
        title:"candy", 
        text:"Orange Candy", 
        ingred :"abc, xyx", 
        price:"$5"
    },
    {
        image:"../assets/images/cake.png",
        title:"cake", 
        text:"Cake", 
        ingred :"pqr, aaa", 
        price:"$10"
    }

];
export default Data;

【问题讨论】:

  • 你是怎么导入的?

标签: javascript reactjs react-native jsx


【解决方案1】:

文档:https://reactnative.dev/docs/image

用法:

    import React, {FC} from 'react';
    import {Image, SafeAreaView, Text} from 'react-native';

    const data = {
      image: require('./src/assets/random-picture.png'),
      text: 'lala random text',
    };

    const App: FC = () => {
      return (
        <SafeAreaView>
          <Image source={data.image} />

          <Text>{data.text}</Text>
        </SafeAreaView>
      );
    };

    export default App;

替代方案:

在根目录下创建types.d.ts 添加

declare module '*.png';
declare module '*.jpg';
declare module '*.svg';

保存

使用进口:

import React, {FC} from 'react';
import {Image, SafeAreaView, Text} from 'react-native';
import RandomImage from './src/assets/random-picture.png';

const data = {
  image: RandomImage,
  text: 'lala random text',
};

const App: FC = () => {
  return (
    <SafeAreaView>
      <Image source={data.image} />

      <Text>{data.text}</Text>
    </SafeAreaView>
  );
};

export default App;

【讨论】:

  • 在 React js 网站上工作,如何在单个组件卡上获取多个数据?
  • 问题太模糊了。
  • 我如何从 data.jsx 文件中获取本地图像作为 React 卡片到我的应用组件!!
猜你喜欢
  • 2015-09-04
  • 1970-01-01
  • 2018-08-20
  • 2015-04-25
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 2011-11-09
  • 2018-02-21
相关资源
最近更新 更多