类型错误:尝试在 require(`${some_var_string}`) 中使用模板文字时,无法将对象转换为原始值

问题描述 投票:0回答:1

尝试在Vite中使用lib“react-inlinesvg”。 我有这样的代码:

<SVG src={require(`../../../assets/icons/icon/logo_${themeProp}_full.svg`)} />;

结果我有:

react-inlinesvg.js?v=ee63b9bd:1002 Uncaught (in promise) TypeError: Cannot convert object to primitive value
    at RegExp.exec (<anonymous>)
    at react-inlinesvg.js?v=ee63b9bd:1002:62
    at react-inlinesvg.js?v=ee63b9bd:1083:7
    at commitHookEffectListMount (chunk-MCEUSTYS.js?v=05c8fdba:16902:34)
    at commitPassiveMountOnFiber (chunk-MCEUSTYS.js?v=05c8fdba:18150:19)
    at commitPassiveMountEffects_complete (chunk-MCEUSTYS.js?v=05c8fdba:18123:17)
    at commitPassiveMountEffects_begin (chunk-MCEUSTYS.js?v=05c8fdba:18113:15)
    at commitPassiveMountEffects (chunk-MCEUSTYS.js?v=05c8fdba:18103:11)
    at flushPassiveEffectsImpl (chunk-MCEUSTYS.js?v=05c8fdba:19484:11)
    at flushPassiveEffects (chunk-MCEUSTYS.js?v=05c8fdba:19441:22)

我在Vite配置中添加了常见的js插件,但它没有改变任何东西

reactjs svg vite commonjs
1个回答
0
投票

检查

themeProp
的值,它应该是一个字符串

© www.soinside.com 2019 - 2024. All rights reserved.