[Vuejs]-Fetch data with vue and sockets

0πŸ‘

βœ…

I have changed the view all users file to:

...
methods: {
  mycall () {
    axios.get('http://localhost:7000/api/users')
     .then(res => {
       const data = res.data
       const users = []
       for (let key in data) {
         const user = data[key]
         user.id = key
         users.push(user)
       }
       this.users = users
     })
     .catch(error => console.log(error))
  },
  socketcall (thecall) {
    this.socket.on(thecall, (data) => {
      this.mycall()
    })
  }
}
....
created () {
  this.mycall()
},
mounted () {
  this.socketcall('user-deleted')
}

Life cycle-hooks cannot retrieved functions inside β€œthis.socket.on” so I thought to do like above and it works!

Leave a comment