【问题标题】:passing require method's argument using variable in React在 React 中使用变量传递 require 方法的参数
【发布时间】:2020-12-02 22:26:32
【问题描述】:

每当我将相应的文件路径存储到变量中时

let source = './products/image/sunglass.jpg'

并将其作为 require 方法的参数传递

require(source)

它给了

模块未找到错误

但如果我按照以下方式编写 require 方法,它就会完美运行。

require('' + source)

为什么会这样......

【问题讨论】:

    标签: javascript node.js reactjs web require


    【解决方案1】:

    这是因为在第一种情况下,您传递的是整个图像而不是其路径,但如果您使用 '' 联系它,那么它会被转换为字符串,这就是它工作正常的原因

    【讨论】:

    • 怎么样?源变量包含一串路径,而不是图像
    猜你喜欢
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    相关资源
    最近更新 更多