Image 在React本机Web应用中未显示图像。为Android或iOS构建时出现,但不';使用react脚本启动时不显示
我正在尝试在使用React脚本启动运行的React本机Web应用程序中显示图像。当我为iOS或Android构建应用程序时,图像看起来非常好(使用expo),但当我为Web应用构建应用程序时,图像无法加载。下面是加载图像的主组件的代码段Image 在React本机Web应用中未显示图像。为Android或iOS构建时出现,但不';使用react脚本启动时不显示,image,react-native,web-applications,Image,React Native,Web Applications,我正在尝试在使用React脚本启动运行的React本机Web应用程序中显示图像。当我为iOS或Android构建应用程序时,图像看起来非常好(使用expo),但当我为Web应用构建应用程序时,图像无法加载。下面是加载图像的主组件的代码段 import React from "react"; import { ScrollView, ActivityIndicator, StyleSheet, Image, ImageBackground } from "react-
import React from "react";
import { ScrollView, ActivityIndicator, StyleSheet, Image, ImageBackground } from "react-native";
import UserList from "./user-list";
import Header from './header';
import sanityClient from './assets/client'
import BackButton from './back-button'
import User from './user'
// import {Asset} from 'expo-asset';
// const imageURI = Asset.fromModule(require('./arrow.png')).uri;
const image = require('./assets/aoeu.jpg');
class Home extends React.Component {
state = {
user: {},
loading: true
};
componentDidMount() {
// TODO: get users
this.getUser();
}
async getUser() {
sanityClient.fetch(`*[ _type == "user" && emailAddress.current == "dwight@viamaven.com"]`)
.then((data) => {
console.log(data);
this.setState({user: data[0], loading: false});
console.log(this.state.user);
})
.catch((err) => console.error(err))
// const res = await fetch("https://randomuser.me/api/?results=20");
// const { results} = await res.json();
// // console.log(results)
// this.setState({users: [...results], loading: false});
}
render() {
return (
<ScrollView
noSpacer={true}
noScroll={true}
style={styles.container}
showVerticalSCrollIndicator = {false}
showHorizontalScrollIndicator = {false}
>
{this.state.loading ? (
<ActivityIndicator
style={[styles.centering, styles.gray]}
color="#5d38aa"
size="large"
/>
) : (
<View>
<Header title={this.state.user.name} />
<View>
<Image
source={require('./arrow.png')}
style={styles.image}
/>
</View>
<User />
</View>
)}
</ScrollView>
);
}
}
var styles = StyleSheet.create({
container: {
backgroundColor: "white",
width: '375px',
height: '812px',
// top: '50px',
},
centering: {
alignItems: "center",
justifyContent: "center",
padding: 8,
height: '100vh'
},
image: {
width: '50px',
height: '50px',
marginRight: 20,
boxShadow: "0 1px 2px 0 rgba(0,0,0,0.1)"
}
});
export default Home;
从“React”导入React;
从“react native”导入{ScrollView、ActivityIndicator、样式表、图像、ImageBackground};
从“/user list”导入用户列表;
从“./头”导入头;
从“./assets/client”导入sanityClient
从“./后退按钮”导入后退按钮
从“./User”导入用户
//从“expo资产”导入{Asset};
//const imageURI=Asset.fromModule(require('./arrow.png')).uri;
const image=require('./assets/aoeu.jpg');
类Home扩展了React.Component{
状态={
用户:{},
加载:正确
};
componentDidMount(){
//TODO:获取用户
这个.getUser();
}
异步getUser(){
sanityClient.fetch(`*[\u type==“user”&&emailAddress.current==”dwight@viamaven.com"]`)
。然后((数据)=>{
控制台日志(数据);
this.setState({user:data[0],loading:false});
console.log(this.state.user);
})
.catch((err)=>console.error(err))
//const res=等待获取(“https://randomuser.me/api/?results=20");
//const{results}=await res.json();
////console.log(结果)
//this.setState({users:[…results],loading:false});
}
render(){
返回(
{this.state.loading(
尝试以这种方式导入
import image = './assets/aoeu.jpg';
如果您正在使用TS,它将有一个错误:
类型“string”不可分配给类型“ImageSourcePropType”。否则它可以正常工作