代码之家  ›  专栏  ›  技术社区  ›  Morton

为什么固定美元不起作用?

  •  0
  • Morton  · 技术社区  · 6 年前

    如果我刚开始 require('../img/dest.png') 图像是有效的,但是当我试图使用 enter image description here

    它会显示错误

    calls to require expect exactly 1 string literal argument but this was found: require(""+item.img+"").(null)
    

    有人知道我的美元变量是怎么回事吗?

    提前谢谢。

    render() {
      // Taken from https://flatuicolors.com/
      const items = [
        { name: 'Name', code: '#1abc9c', url: 'yahoo.com.tw', img: '../img/dest.png' }
      ];
    
      return (
        <GridView
          itemDimension={130}
          items={items}
          style={styles.gridView}
          renderItem={item => {
            console.log(`'${item.img}'`);
            return (
            <TouchableOpacity onPress={() => this.showLinkAlert(item.name, item.url)}>
              <ImageBackground source={require(`'${item.img}'`)} style={[styles.itemContainer, { backgroundColor: '#bdc3c7' }]}>
               <Text style={styles.itemName}>{item.name}</Text>
              </ImageBackground>
            </TouchableOpacity>
            );
          }}
        />
      );
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Johanna Larsson    6 年前

    字符串替换在运行时发生,但是 require 在构建时发生。所以当require运行时,它不会替换任何东西,它只是试图找到一个名为that的文件。