在 Phaser 3.24.1 游戏中使用 Axios 库

Using Axios Library In Phaser 3.24.1 Game

我在 Phaser 3 游戏中的 axios 调用中创建了一个小的 GET 请求,只是为了测试它是否有效:

axios.get('/fetch-new-sudoku')
    .then(function (response) 
    {
        axiosExperiment = response.data.message;
        console.log(response.data, axiosExperiment);
    })
    .catch(function (error) 
    {
        console.log(error);
    })
    .then(function () 
    {
        // always executed
    });

在我的 Laravel 7 控制器中,我有以下内容:

public function APIFetchNewSudoku()
{
    $data['message'] = "Success";

    return response()->json($data);
}

当我打开浏览器检查时,我看到请求正常触发,并在控制台中显示“成功”消息。当我尝试将响应数据消息分配给 var axiosExperiment 时,它根本不起作用?我在我的代码前面为 axiosExperiment 变量分配了一个空字符串值,如下所示:

var axiosExperiment = "";

我希望能够将响应数据消息分配给 axiosExperiment,但我真的不知道我做错了什么? var 仍然等于 ""?

您没有足够的信息了解您如何使用 axiosExperiment 我猜你在承诺解决之前调用了 axiosExperiment

尝试在你的移相器 update() 上设置文本(axiosExperiment ? axiosExperiment : '')

create(){
    var self = this
    this.axiosExperiment = ''
    this.text = this.add.text(0,0, axiosExperiment)

    axios.get('/fetch-new-sudoku')
    .then(function (response) 
    {
        self.axiosExperiment = response.data.message;
        console.log(response.data, axiosExperiment);
    })
}
update(){
     this.text(this.axiosExperiment)
}