当我在app.js文件中使用ES6类Tibia
时,出现以下错误:
未捕获的ReferenceError:未定义胫骨。
app.js文件:
const tibia = new Tibia;
document.getElementById('search-form').addEventListener('submit', (e) => {
const searchedCharacter = document.getElementById('searchCharacter').value;
tibia.getCharacter(searchedCharacter);
e.preventDefault();
});
类文件:
class Tibia {
async getCharacter(char) {
const characterResponse = await
fetch(`https://api.tibiadata.com/v2/characters/${char}.json`);
const character = await characterResponse.json();
return {
character
}
}
}
答案 0 :(得分:0)
您应该导出Tibia
类,就像在类文件中那样:
export { Tibia };
您应该将Tibia
类导入app.js
,如下所示:
import { Tibia } from 'path-to-file.js';
或者那样:
export default Tibia;
并且:
import Tibia from 'path-to-file.js';