У меня есть простое приложение, встроенное в 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"]
}