最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js swap array items - Stack Overflow

programmeradmin2浏览0评论

In my vue.js application I'm trying to swap 2 forum rows like this:

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;
                    });
            }
        }
    }

But nothing happens? What am I doing wrong here?

In my vue.js application I'm trying to swap 2 forum rows like this:

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;
                    });
            }
        }
    }

But nothing happens? What am I doing wrong here?

Share Improve this question asked Jan 25, 2017 at 16:50 JenssenJenssen 1,8715 gold badges44 silver badges76 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 21

While @dfsq is correct about the use of index++ Vue doesn't recognize native mutations of arrays due to the inability to observe them. you have to use a mutation method to change them.

try this:

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

I haven't tested it and I'll edit when I can.

发布评论

评论列表(0)

  1. 暂无评论