【问题标题】:Randomize/shuffle order of elements in Vue.js on page load页面加载时 Vue.js 中元素的随机/随机顺序
【发布时间】:2020-06-28 02:27:24
【问题描述】:

我正在尝试使用 Vuejs ref 属性来定位一些元素,以随机化每个页面加载的顺序。

我的数据在模板中渲染并在组件中处理:

<div class="team" >
  <div class="team__card" ref="card" v-for="(member, index) in team"
  :key="index">
  <div v-for="(image, imageIndex) in member.image" :key="imageIndex">
    <img :src="image" alt="Photo of team member" :key="imageIndex" />
  </div>
  <div class="team__card-content">
    <p class="font-bold text-xl">{{ member.name }}</p>
    <p class="font-bold text-gray-700">{{ member.title }}</p>
    <p class="text-gray-700">{{ member.description }}</p>
  </div>
 </div>
</div>

<script>
export default {
name: 'Page Name',
data() {
 return {
  team: [
   {
     image: [require('url')],
     name: 'Name',
     title: 'Title',
     description:'description.'
   },
   {
     image: [require('url')],
     name: 'Name',
     title: 'Title',
     description:'description.'
   },
  ]
 }
},
created() {
    this.randomize()
  },
  methods: {
    randomize() {
      for (let i = this.team.length - 1; i > 0; i--) {
        let randomIndex = Math.floor(Math.random() * i)
        let temp = this.team[i]
        this.set(this.team, i, this.team[randomIndex])
        this.set(this.team, randomIndex, temp)
      }
    }
  }
}
</script>

我在这里缺少什么?我如何在我的卡片元素 TIA 的每个页面加载上洗牌/随机排序!

【问题讨论】:

  • 你在v-for 中有card 引用吗(即有多个实例)?如果是这样,我相信$refs.card 将是一个数组,因此不会有eq 方法
  • 您的浏览器控制台中是否有任何错误消息?
  • 没有错误信息!
  • 我相信如果你想要一个引用数组,你必须使用v-for
  • 好的,我会看看如何做到这一点。目前我的数据结构在模板中处理。我知道如何在 js 或 jquery 中定位这些,但不知道 DOM 操作如何在 vuejs 中工作

标签: javascript arrays vue.js dom ref


【解决方案1】:

this.set 调用在set 之前缺少$。如果没有$,则会导致set 不是函数的运行时错误。

this.$set(this.team, i, this.team[randomIndex])
this.$set(this.team, randomIndex, temp)

也可以使用Vue.set;但是由于 set 在可以访问 Vue 实例的函数内被调用,所以 $set 将是首选。

工作示例:

https://codesandbox.io/s/eager-cori-cu75f

【讨论】:

    【解决方案2】:

    您不应该像那样手动操作 DOM,因为 Vue 会在下一次更新期间覆盖它(假设 Vue 没有被这些更改弄糊涂)。

    似乎没有任何理由应该像在模板中那样对这些卡片进行硬编码。您应该在组件数据中定义的数组中定义这些卡片的数据,然后使用v-for 将其渲染出来,然后您所要做的就是shuffle the array。

    这是一个演示:

    // This is an in-place array shuffle function which is
    // compatible with arrays observed by Vue
    function shuffleVueArray(array) {
      for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        const temp = array[i];
        Vue.set(array, i, array[j]);
        Vue.set(array, j, temp);
      }
    }
    
    new Vue({
      el: '#app',
      data: {
        items: [
          'apple',
          'banana',
          'orange',
          'pear',
          'pineapple',
          'mango',
        ],
      },
      methods: {
        randomize() {
          shuffleVueArray(this.items);
        },
      },
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    
    <div id="app">
      <button @click="randomize">Randomize</button>
      <ul>
        <li v-for="item of items">{{ item }}</li>
      </ul>
    </div>

    【讨论】:

    • 我添加了一个演示。请务必阅读change detection caveats。
    • 只需在 created 挂钩中调用 this.randomize()。
    • 谢谢,但我已经更新了我的问题,但功能仍然无法正常工作。生病继续挖掘
    • this.team(target).before 不起作用,也许你认为它是一个 jQuery 对象? this.team 是一个数组。应该没有 DOM 操作。
    • 更新了不依赖全局 vue 实例的函数,因为它在组件内部。我还是 vue 和 js 的新手,这就是为什么我仍然很难调试
    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 2015-04-09
    • 1970-01-01
    相关资源
    最近更新 更多