javascript - Remove item from array on timeout - Stack Overflow

I am creating a ponent to display notifications that should dismiss automatically after a few seconds i

I am creating a ponent to display notifications that should dismiss automatically after a few seconds in Vue, my alert ponents emits an 'expired' event and then I listen for this event in the parent, and remove it from the parent data array with splice, this works sometimes but sometimes the 'alerts' are not removed.

Vueponent('alert', {
  template: '<li><slot></slot></li>',
  mounted() {
    setTimeout(() => this.$emit('expired'), 2000)
  }
});

new Vue({
  el: '#app',
  data: {
    count: 0,
    alerts: []
  },
  methods: {
        createAlert(){
        this.alerts.push(this.count++)
      },
      removeItem(index) {
        this.alerts.splice(index, 1)
      }
  }
});

See this Fiddle and click on the Create Alert button a couple of times, and some of the alerts won't get dismissed. Any ideas on how to solve this?

I am creating a ponent to display notifications that should dismiss automatically after a few seconds in Vue, my alert ponents emits an 'expired' event and then I listen for this event in the parent, and remove it from the parent data array with splice, this works sometimes but sometimes the 'alerts' are not removed.

Vue.ponent('alert', {
  template: '<li><slot></slot></li>',
  mounted() {
    setTimeout(() => this.$emit('expired'), 2000)
  }
});

new Vue({
  el: '#app',
  data: {
    count: 0,
    alerts: []
  },
  methods: {
        createAlert(){
        this.alerts.push(this.count++)
      },
      removeItem(index) {
        this.alerts.splice(index, 1)
      }
  }
});

See this Fiddle and click on the Create Alert button a couple of times, and some of the alerts won't get dismissed. Any ideas on how to solve this?

Share Improve this question asked Mar 27, 2017 at 1:21 enriqg9enriqg9 1,5071 gold badge22 silver badges42 bronze badges 4
  • Hint: When an item is removed from an array, what happens to the indices of the items after it? – nnnnnn Commented Mar 27, 2017 at 1:34
  • @nnnnnn they reset and start from 0 again, but then how can I delete an specific item if there are no associative arrays in javascript? – enriqg9 Commented Mar 27, 2017 at 1:44
  • 2 I don't know enough about Vue to know what the "approved" Vue approach is, but JS does have objects, so perhaps one approach would be to have an array of objects with an id and text property (or whatever), and in the remove function search the array for the object with the right id. – nnnnnn Commented Mar 27, 2017 at 1:50
  • @nnnnnn Exactly. – Bert Commented Mar 27, 2017 at 1:54
Add a ment  | 

1 Answer 1

Reset to default 7

As mentioned in the ments, don't do this by index. Here is one alternative.

<div id="app">
  <button @click="createAlert">
    Create Alert
  </button>
  <alert v-for="(alert, index) in alerts" :key="alert.id" :alert="alert" @expired="removeItem(alert)">{{ alert.id }}</alert>
</div>

Vue.ponent('alert', {
  props: ["alert"],
  template: '<li><slot></slot></li>',
  mounted() {
    setTimeout(() => this.$emit('expired', alert), 2000)
  }
});

new Vue({
  el: '#app',
  data: {
    count: 0,
    alerts: []
  },
  methods: {
        createAlert(){
        this.alerts.push({id: this.count++})
      },
      removeItem(alert) {
        this.alerts.splice(this.alerts.indexOf(alert), 1)
      }
  }
});

Your fiddle revised.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745178993a4615329.html

相关推荐

  • javascript - Remove item from array on timeout - Stack Overflow

    I am creating a ponent to display notifications that should dismiss automatically after a few seconds i

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信