Vue.js交换数组项



在我的 vue.js 应用程序中,我正在尝试交换 2 个论坛行,如下所示:

export default {
        data() {
            return {
                forums: []
            }
        },
        methods: {
            increment(forum, index) {
                ForumService.increment(forum)
                    .then(() => {
                        let b = this.forums[index];
                        this.forums[index] = this.forums[index++];
                        this.forums[index++] = b;
                    });
            }
        }
    }

但是什么也没发生?我在这里做错了什么?

虽然@dfsq对index++的使用是正确的,但由于无法观察到它们,Vue 无法识别数组的原生突变。 您必须使用突变方法来更改它们。

试试这个:

.then(() => {
  let rows = [this.forums[index], this.forums[index + 1]];
  this.forums.splice(index, 2, rows[1], rows[0] );
});

我还没有测试过它,我会在可以的时候进行编辑。

最新更新