V-for in vue traverses the array, and after disturbing the array sorting, the view is not updated?

v-for traverses the array, and after disturbing the array sorting, the view is not updated

wanted to do a Vue official ide/transitions.html-sharp%E5%A4%9A%E4%B8%AA%E5%85%83%E7%B4%A0%E7%9A%84%E8%BF%87%E6%B8%A1" rel=" nofollow noreferrer > list transition effect, but found that v-for traverses the array, disrupting the array sorting, the view will not be updated?

related codes

<template>
  <div class="page-item section">
    <h2>skill</h2>
    <mu-container class="container">
      <transition-group name="flip-list" tag="div">
        <!-- <template> -->
          <mu-chip v-for="(item, index) in skills" class="chip" :key="index" color="-sharp009688">
            {{item}}
          </mu-chip>
        <!-- </template> -->
      </transition-group>
      <button @click="shuffle">shuffle</button>
    </mu-container>
  </div>
</template>
<script>
import shuffle from "shuffle-array"
import {
  mapState,
  mapActions
} from "vuex"
export default {
  data () {
    return {
      skills: ["vue", "react", "js", "html", "css", "node"]
    }
  },
  computed: {
    // ...mapState(["skills"])
  },
  methods: {
    // ...mapActions(["shuffleSkills"]),
    shuffle () {
      console.log(this.skills)
      const oldSkills = this.skills
      this.skills = shuffle(this.skills)
      console.log(this.skills)
      console.log(this.skills === oldSkills)
      // this.skills.push("Vue")
      // this.shuffleSkills()
    }
  }
}
</script>

clipboard.png

shuffle

2

Push"Vue"


clipboard.png

you can see from the diagram that the order on the view has changed. this means that changing the order of the array elements alone does not make the v-for view update

.

but the official example does require only the sorting of shuffle primitive array elements, and the view is automatically updated. If I don"t change the contents of the array, how can I make the view update if I just change the sorting of the array elements?

Sep.26,2021

you make a deep copy of the global array you defined

for example:

this.list = JSON.parse(JSON.stringify(this.list))

the subject has solved the problem by himself, so I will provide my solution through this problem.

    The main problem with
  1. is that Vue does not detect array changes.
  2. prerequisites for Vue to monitor array changes:

    • replace array (pointer to other array).
    • use the array variation method.
    • uses set to assign values to array items.

so part of the code analysis for processing the array.

  1. if the aim is to modify the array using the mutation method, you need to make sure that the ide/list.html-sharp%E5%8F%98%E5%BC%82%E6%96%B9%E6%B3%95" rel=" nofollow noreferrer "> mutation method is used
  2. if the purpose is to replace the array, make sure that the replaced array is the same object as the original array, and make sure that it is replace , not repeat assignment .
  3. if you want to set an item directly using the index, you cannot modify the value directly. Instead, you need to cooperate with the set method provided by Vue to ide/list.html-sharp%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9" rel=" nofollow noreferrer "> modify .

is caused by the same key in the dom list before and after the array transformation, because you are using the array subscript as the key here.


laxative ~
first of all Vue official documentation says which array changes cannot be detected to trigger view updates, ide/list.html-sharp%E6%95%B0%E7%BB%84%E6%9B%B4%E6%96%B0%E6%A3%80%E6%B5%8B" rel=" nofollow noreferrer "> portal .
then looks at the source code of the shuffle-array library referenced by the master, and finds that this method is precisely used in the method called to change the array: portal . The code is as follows:

....
while (len) {
    random = Math.floor(rng() * len);
    len -= 1;
    temp = collection[len];
    // 
    collection[len] = collection[random];
    collection[random] = temp;
  }
...
< hr >

if it is helpful, please click to adopt it, thank you ~

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e45c04-43ad5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e45c04-43ad5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?