简体   繁体   English

在调用完成之前,如何停止渲染vue组件

[英]How do I stop my vue component from rendering until a call completes

I've googled this but I can't find any specific solution. 我用谷歌搜索了这个,但我找不到任何具体的解决方案。 Basically I have a vue component that depends on an init call and I want it to stop rendering until the call completes, at which point I want the component to render. 基本上我有一个依赖于init调用的vue组件,我希望它在调用完成之前停止渲染,此时我希望组件呈现。 Seems simple but unless I'm missing something I can't find any lifecycle method that does that. 看似简单,但除非我遗漏了一些东西,否则找不到任何生命周期方法。

You can use v-if for that purpose 您可以为此目的使用v-if

<template>
    <div v-if="loaded"></div>
</template>

<script>
    export default {
        name: 'TestComponent',
        data: function () {
            return {
                loaded: false
            }
        },
        created() {
          callExternalServices().then(() => {
             this.loaded = true
          })
        }
    }
</script>

It will render an empty component until loaded == true 它将呈现一个空组件,直到loaded == true

Basically you make an init call in the created or mounted lifecycle method and you initialize a data object with the response of the call. 基本上,您在createdmounted生命周期方法中进行初始化调用,并使用调用的响应初始化data对象。 If you don't change data during the call there is no reason for vue to render anything. 如果在调用期间没有更改数据,则没有理由让vue呈现任何内容。

Maybe you can provide a jsfiddle that show exactly your problem. 也许你可以提供一个显示你的问题的jsfiddle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM