未捕获的ReferenceError:未定义Tibia

时间:2018-08-09 18:25:31

标签: javascript promise

当我在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
        }
    }
}

1 个答案:

答案 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';