React native 类型错误:undefined不是对象(计算';style.width';)

React native 类型错误:undefined不是对象(计算';style.width';),react-native,React Native,我想把汽车图像放在背景图像上,仅此而已 import React, { Component } from 'react'; import { ... Animated, ImageBackground } from 'react-native'; export default class App extends Component { render() { return ( <ImageBackground imageStyle={styles.cont

我想把汽车图像放在背景图像上,仅此而已

import React, { Component } from 'react';
import {
...
  Animated,
  ImageBackground
} from 'react-native';


export default class App extends Component {
  render() {
    return (
      <ImageBackground imageStyle={styles.container} source={require('./src/assets/img/road1.jpg')}>
       <Animated.Image style={styles.car} source={require('./src/assets/img/car2.png')}>
       </Animated.Image>
      </ImageBackground>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    position: 'relative',
    resizeMode: 'cover'
  },
  car:{
    position:'absolute',
    width:100,
    height:50
  }
});
好的,但为什么它需要“style.width”,即使我使用了resizeMode:“cover”
我还在style object中设置了宽度和高度属性,但仍然存在相同的错误。。我怎样才能解决这个问题

您的错误来自ImageBackground.js

您正在为ImageBackground提供一个容器样式作为道具,它要求在其中显示宽度

您正在通过无效的prop
imageStyle
提供样式。它应作为
样式提供

尝试:


Type error:undefined is not an object(evaluating 'style.width')
<ImageBackground style={styles.container} source={require('./src/assets/img/road1.jpg')}>
       <Animated.Image style={styles.car} source={require('./src/assets/img/car2.png')}>
       </Animated.Image>
</ImageBackground>