Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display array length dynamically

I am trying to display websocket messages received with index and total number. How can I display total dynamically?

<template>
  <div>
    <v-btn @click="clickButton">Test Websocket</v-btn>
    <h1>Total webhooks : {{total}}</h1>
    <ul v-for="(log,index) in logs" :key=index >
      <li>{{index}} - {{ log }}</li>
    </ul>
  </div>
</template>

<script>
    export default {
      data () {
        return {
          logs: [],
          newlog: '',
          total:0
        }
      },
      methods: {
        clickButton: function () {
          this.$socket.send('Let me ask you a questin.can u hear me???')
        }
      },
      created () {
        this.$options.sockets.onmessage = (data) => {
          console.log(data)
          this.logs.push(data.data)
        }
      },
    }
</script>
like image 583
Abdulla Thanseeh Avatar asked Jun 26 '26 13:06

Abdulla Thanseeh


1 Answers

You don't need a computed property there. You can simply use {{logs.length}} in instead of total

here is a sample fiddle

https://jsfiddle.net/czgnv53q/3/

like image 194
Shanavas M Avatar answered Jun 28 '26 12:06

Shanavas M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!