首页 > 解决方案 > React-Native undefined 无法解析模块

问题描述

我正在尝试导入我在打字稿中制作的类,并将其初始化为与我的视图组件一起传递。但是当我尝试初始化类时,出现以下错误:

模拟器中的错误消息

我的导入如下所示:

import GameModel from 'src/Rematch/Models/GameModel';

我的 GameModel 类如下所示:

import { QuestionModel } from "./QuestionModel";

export default class GameModel {

    public questions: QuestionModel[] = [];

    constructor(private id: number = 0) {
        this.getData(id);
    }

    getData(id: number) {
        console.log(id);
        this.questions.push(new QuestionModel(['Goed', 'Fout', 'Nog fouter'], 'Goed', 'Dit is de eerste vraag. Als je deze goed hebt krijg je punten'));
        this.questions.push(new QuestionModel(['Fout', 'Goed', 'Foutig'], 'Goed', 'Dit is de tweede vraag. Als je deze goed hebt krijg je punten'));
        this.questions.push(new QuestionModel(['Fout', 'Fouter dan fout', 'Goed'], 'Goed', 'Dit is de derde vraag. Als je deze goed hebt krijg je punten'));
        this.questions.push(new QuestionModel(['Dit is ook fout', 'Goed', 'Fout'], 'Goed', 'Dit is de vierde vraag. Als je deze goed hebt krijg je punten'))
    }
}

打字时:

const game: GameModel = new GameModel(1)

出现错误

标签: typescriptreact-native

解决方案


您的导入必须与您编写的文档相关,因此应该是这样的:

import GameModel from './Rematch/Models/GameModel';

推荐阅读