【发布时间】:2020-05-12 23:51:57
【问题描述】:
我有一个内置在 CRA v3.2.0 中的简单应用程序。我使用默认设置的 TypeScript(参见下面的tsconfig.ts)。在/src/sounds/ 中,我有一个要在组件中使用的 mp3 文件“click_hi.mp3”,但我无法导入 mp3 并出现错误 Cannot find module 'sounds/click_hi.mp3'.ts(2307)。
我尝试将文件放在与组件相同的文件夹中,以确保避免路径中的拼写错误。但是运气不好...如何导入 mp3 文件?
App.tsx
import React, { useState, useRef, useEffect } from "react";
import ReactDOM from "react-dom";
import { Sampler } from "tone";
import ClickHi from 'sounds/click_hi.mp3'
export default function ExercisePage() {
const [isLoaded, setLoaded] = useState(false);
const sampler = useRef(null);
useEffect(() => {
sampler.current = new Sampler(
{ "A1": ClickHi },
{
onload: () => {
setLoaded(true);
}
}
).toMaster();
}, []);
const handleClick = () => sampler.current.triggerAttack("A1");
return (
<div>
<button disabled={!isLoaded} onClick={handleClick}>
start
</button>
</div>
);
}
tsconfig.ts
{
"compilerOptions": {
"baseUrl": "src",
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": ["src"]
}
【问题讨论】:
-
var ClickHi = require('file-loader!./sounds/click_hi.mp3');这可能会有所帮助。 -
“导入”一个 mp3 文件会是什么样子?您在寻找原始数据吗?
标签: reactjs typescript create-react-app