vue.js - 在 vue.js 中获取来自 axios 的响应数据

标签 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 .

最佳答案

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

关于vue.js - 在 vue.js 中获取来自 axios 的响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45017874/

相关文章:

javascript - 来自 API 调用的 VueJS 数据不显示

javascript - 如何使用过滤器在数组中的对象的多个键值中进行搜索?

reactjs - app.delete 中的 req.params 为空,在 react 中使用 express、axios

javascript - Reactjs:无限滚动不起作用

javascript - React axios 发送多张图片表单数据

vue.js - 将数据传递到 Vue 模板

vue.js - VueJS v-if 用于禁用/启用按钮

vue.js - import axios 导致 vue v3 和 vite 出现问题

laravel - 从 axios 响应中获取数据(Vuejs)

reactjs - React Native - 在功能组件中创建方法并在组件外部调用此方法,可能吗?