简体   繁体   English

无法导入图像:InvalidCharacterError:无法在“文档”上执行“createElement”:提供的标签名称不是有效名称

[英]Unable to import images: InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided is not a valid name

I'm very new to React Native, and I'm trying to import an image into my app but I keep gettin this error:我对 React Native 很陌生,我正在尝试将图像导入我的应用程序,但我一直遇到这个错误:

InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided ('/static/media/logo.ab75c327.png') is not a valid name. InvalidCharacterError:无法在“文档”上执行“createElement”:提供的标签名称(“/static/media/logo.ab75c327.png”)不是有效名称。

This is my code right now:这是我现在的代码:

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

import style from './styles';
import Logo from '../../../assets/images/logo.png';

export default () => {
    return (
        <View>
            <Logo />
        </View>
    );
}   

It should be something super simple, but for some reason it isn't working for me.它应该是超级简单的东西,但由于某种原因它对我不起作用。 I also tried using <Image source={Logo} /> as well, but still doesn't work.我也尝试过使用<Image source={Logo} /> ,但仍然无法正常工作。

I then decided not to import the Logo and tried <Image source='../../../assets/images/logo.png' /> .然后我决定不导入徽标并尝试<Image source='../../../assets/images/logo.png' /> When I do this, I no longer get any errors, but the image simply doesn't show up.当我这样做时,我不再收到任何错误,但图像根本不显示。

I have tried with other images and other formats as well, and in all cases I end up getting the same results.我也尝试过其他图像和其他格式,并且在所有情况下我最终都得到了相同的结果。 What am I doing wrong?我究竟做错了什么?

I'm not a react programmer but I see the javascript import statement is for modules, not images locations.我不是反应程序员,但我看到 javascript 导入语句适用于模块,而不是图像位置。 That might have something to do with it and gels with your errors.这可能与它有关,并且与您的错误有关。

See mozilla javascript docs here for more info on the javascript import syntax.有关 javascript 导入语法的更多信息,请参阅此处的 mozilla javascript 文档。

Cheers.干杯。

You need to import the image using the <img> tag, like:您需要使用<img>标签导入图像,例如:

<img src={Logo} alt='logo'/> 

Your code corrected:您的代码已更正:

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

import style from './styles';
import Logo from '../../../assets/images/logo.png';

export default () => {
    return (
        <View>
            <img src={Logo} alt='logo'/>
        </View>
    );
}   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidCharacterError:无法在“文档”上执行“createElement”:提供的标记名称(“/static/media/tab1.fab25bc3.png”)不是有效名称 - InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided ('/static/media/tab1.fab25bc3.png') is not a valid name React Uncaught DOMException:无法在“文档”上执行“createElement”:提供的标签名称(“<div> &#39;) 不是有效名称 - React Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('<div>') is not a valid name 未捕获的 DOMException:无法在“文档”上执行“createElement”:提供的标签名称 - Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided 我收到以下错误... [Uncaught InvalidCharacterError:无法在&#39;Element&#39;上执行&#39;setAttribute&#39;:&#39;0&#39;不是有效的属性名称 - I got the following error… [Uncaught InvalidCharacterError: Failed to execute 'setAttribute' on 'Element': '0' is not a valid attribute name d3 react js-无法在“文档”上执行“ createElementNS”:提供的限定名称包含无效字符“,” - d3 react js - Failed to execute 'createElementNS' on 'Document': The qualified name provided contains the invalid character ',' 无法在“元素”上执行“setAttribute”:“roman”;“”不是有效的属性名称 - Failed to execute 'setAttribute' on 'Element': 'roman";"' is not a valid attribute name 无法在“元素”上执行“setAttribute”:“[编辑器]?” 不是有效的属性名称 - Failed to execute 'setAttribute' on 'Element': '[editor]?' is not a valid attribute name 错误 DOMException:无法在“元素”上执行“setAttribute”:“{{”不是有效的属性名称 - ERROR DOMException: Failed to execute 'setAttribute' on 'Element': '{{' is not a valid attribute name 使用空格修改 css 名称时出现 Javascript InvalidCharacterError - Javascript InvalidCharacterError when modifying a css name with a space Angularjs oauth错误:无法对&#39;XMLHttpRequest&#39;执行&#39;setRequestHeader&#39;:&#39;Authorization:Bearer&#39;不是有效的HTTP标头字段名称 - Angularjs oauth Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Authorization: Bearer ' is not a valid HTTP header field name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM