我的需求:根据 path 匹配,动态引入 ts 文件(其实是自己定义的静态数据)
现实问题:import().then 用变量时会报错
let { name } = useParams()
const [bookData, setBookData] = useState<string[]>()
useEffect(() => {
if (name !== undefined) {
import(`@/store/${name}`)
.then(module => {
// @ts-ignore
const data: string[] = module[data]
setBookData(data)
})
}
}, [])
上面这样写会报错 Uncaught (in promise) TypeError: Failed to resolve module specifier '@/store/teach'
但是把import(@/store/${name}) 换成 import('@/store/teach')就正常了。但这样并不能满足根据变量导入的需求呀,求大神指出正确方式。