0

I have a component here, and I need first to make a request using socket.io :

<template>
    <h1>Don't show me before the socket's response</h1>
</template>

<script>
    export default {
        beforeCreate: function() {
            let sessid = this.$cookie.get('sessid')
            this.$options.sockets.logout = (data) => {
                if (data.redirect) {
                    this.$router.push(data.redirect)    
                } else {
                    console.log('here, you can render the template')
                }
            }
            this.$socket.emit('logout', { sessid })
        }
    }
</script>

This code works, but it shows the template in browser for a quick moment, before the redirection happens.

I would like to know if there's a tick to wait the socket response for rendering the template.

1 Answer 1

1

You can use v-if, when the socket response arrives, you can set a variable which can be used with v-if to not show the HTML, something like following:

<template>
    <h1 v-if="sockResp">Don't show me before the socket's response</h1>
</template>

<script>
    export default {
        data: function() {
           return {
             sockResp: false
           }
        },
        beforeCreate: function() {
            let sessid = this.$cookie.get('sessid')
            this.$options.sockets.logout = (data) => {
                if (data.redirect) {
                    this.$router.push(data.redirect)    
                } else {
                    console.log('here, you can render the template')
                    this.sockResp = true 
                }
            }
            this.$socket.emit('logout', { sessid })
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.