在 vue.js 中从 axios 获取响应数据

Get response data from axios in vue.js

我正在尝试从我的 REST API 获取响应数据并将其显示在我的 vue.js 应用程序中,如下所示:

var database = new Vue({
    el: '#database',
    data: {
        databaseConfiguration: {
            type: '',
            url: '',
            port: '',
            username: '',
            password: ''
        },
        errors: []
    },
    created: function () {
        axios.get('/config/database')
            .then(function (response) {
                this.databaseConfiguration = response.data;
                console.log(response.data);
            })
            .catch(function (error) {
                this.errors.push(error);
                console.log(error);
            })
    }
}
)

如果我对其进行调试,我会看到响应是从 REST API 中正确获取的。但不幸的是数据没有显示在 html 页面上。 html 代码如下所示:

<div id="database" class="panel panel-default panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">Database Configuration</h3>
    </div>
    <div class="panel-body">
        <form class="form-horizontal">
            <div class="form-group">
                <label for="inputType" class="col-sm-2 control-label">Type</label>
                <div class="col-sm-10">
                    <input type="text" v-model="databaseConfiguration.type" class="form-control" id="inputType"
                           placeholder="Database type">
                </div>
            </div>
            <div class="form-group">
                <label for="inputUrl" class="col-sm-2 control-label">Url</label>
                <div class="col-sm-10">
                    <input type="text" v-model="databaseConfiguration.url" class="form-control" id="inputUrl"
                           placeholder="Url">
                </div>
            </div>
            <div class="form-group">
                <label for="inputPort" class="col-sm-2 control-label">Port</label>
                <div class="col-sm-10">
                    <input type="number" v-model="databaseConfiguration.port" class="form-control" id="inputPort"
                           placeholder="Port">
                </div>
            </div>
            <div class="form-group">
                <label for="inputUsername" class="col-sm-2 control-label">Username</label>
                <div class="col-sm-10">
                    <input type="text" v-model="databaseConfiguration.username" class="form-control"
                           id="inputUsername" placeholder="Password">
                </div>
            </div>
            <div class="form-group">
                <label for="inputPassword" class="col-sm-2 control-label">Password</label>
                <div class="col-sm-10">
                    <input type="password" v-model="databaseConfiguration.password" class="form-control"
                           id="inputPassword" placeholder="Password">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-default">Save</button>
                </div>
            </div>
        </form>
    </div>
</div>

所有代码都可以找到here

您的 this 在您的回调中指向了错误的对象。尝试使用箭头函数、闭包或 bind.

这是一个使用箭头函数的例子。

axios.get('/config/database')
        .then(response => {
            this.databaseConfiguration = response.data;
            console.log(response.data);
        })
        .catch(error =>{
            this.errors.push(error);
            console.log(error);
        })

How to access the correct this inside a callback