【问题标题】:Call vuejs component's method in jquery closure在 jquery 闭包中调用 vuejs 组件的方法
【发布时间】:2018-08-21 08:23:15
【问题描述】:

这是我的 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 Uncaught TypeError: this.addRow is not a function

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

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

【问题讨论】:

    标签: jquery vue.js vuejs2 closures vue-component


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-04-09
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 2021-06-09
      • 2022-10-04
      • 2020-01-07
      • 2017-12-18
      • 2021-03-14
      相关资源
      最近更新 更多