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

时间:2017-07-10 17:04:06

标签: vue.js axios

我正在尝试从我的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

1 个答案:

答案 0 :(得分:3)

您的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