在 jquery 闭包中调用 vuejs 组件的方法



这是我的vuejs组件:

<script>
export default {
    props: ['columns', 'records', 'group', 'users'],
    data: function () {
        return {
            new_record: true,
            myrecords: this.records
        }
    },
    methods: {
        addRow: function () {
            try {
                console.log(this.myrecords);
                this.myrecords.push({});
                console.log(this.myrecords);
            } catch (e) {
                console.log(e);
            }
        },
        saveRow: function () {
            $.post("http://localhost/someurl", { somedata: somevalue })
                .done(function (data) {
                    console.log(data);
                    this.addRow();
                })
                .fail(function (error) {
                    console.log(error);
                    alert("error");
                });
...
...

错误: app.js:155 未捕获的类型错误: this.addRow 不是函数

我理解为什么会发生这种情况,因为当前上下文中的this是 jquery 对象,

但问题是我如何调用我的 vue 组件的 addRow 方法?

你需要在调用 ajax 之前添加let self = this。然后,您可以拨打self.AddRow()

最新更新