简体   繁体   English

如何将Microsoft SQL Server Client for Node.js响应数据绑定到Vue.js数据对象

[英]How to bind Microsoft SQL Server client for Node.js response data to Vue.js data object

I'm using Microsoft SQL Server client for Node.js ( https://www.npmjs.com/package/mssql ) to fetch data from my SQL server 我正在使用Microsoft SQL Server Client for Node.js( https://www.npmjs.com/package/mssql )从SQL Server中获取数据

Here is my client-side code 这是我的客户端代码

var demo = new Vue({
        el: '#demo',
        data: {
            searchQuery: '',
            gridColumns: ['Source', 'SourceNo', 'TaskType'],
            gridData: []
        },
        mounted: function () {
            this.loadData();
        },
        methods: {
            loadData: function () {
                var ip = location.host;
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: 'http://' + ip + '/sql',
                    data: {
                    },
                    success: function (responseData) {
                        this.gridData = responseData.recordsets;
                        console.log(this.gridData);
                    },
                    error: function (error) {
                        console.log('error', error);
                    }
                })
            }
        }
    })

Here is the responseData from my server 这是我服务器的responseData

来自服务器的响应对象

Unfortunately, I don't get what object should I bind to this.gridData 不幸的是,我没有将什么对象绑定到this.gridData

In your success callback, this no longer represents the Vue instance so you're not actually setting your observable gridData . 在您的success回调中, this不再代表Vue实例,因此您实际上并未设置可观察的gridData

Grab a reference to the current instance and use it inside your callback: 获取对当前实例的引用,并在回调中使用它:

var self = this;
$.ajax(...)
    success: function(responseData) {
        self.gridData = ...
    ...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM