简体   繁体   English

我们可以在 Javascript React Native 中使用 import 或 require 中的变量吗

[英]Can we use variable in import or require in Javascript React Native

i am try to use two variable in require and import but they are not working as i expecting我尝试在 require 和 import 中使用两个变量,但它们没有按我预期的那样工作

class SvgIllustration extends Component {
    render() {
        const { selectedLanguage = "en", svgName } = this.props;
        const location = `../assets/svg/${ selectedLanguage }/${ svgName }.svg`;
        return <SvgCss
            xml={ require( location ) }
        />;
    }

}

Error i get:我得到的错误:

error: bundling failed: src\\components\\svgIllustration.js: src\\components\\svgIllustration.js:Invalid call at line 10: require(location)错误:捆绑失败:src\\components\\svgIllustration.js:src\\components\\svgIllustration.js:第 10 行无效调用:需要(位置)

You cant do this :你不能这样做:

const location = `../assets/svg/${ selectedLanguage }/${ svgName }.svg`;

Dynamic imports are not allowed.不允许动态导入。 you need to import location first with proper require like您需要先使用适当的要求导入位置,例如

xml={ require( ../assets/svg/english/wow.svg ) }

hope it helps希望能帮助到你

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM