Subscribe on changes!

异步返回后端数据渲染组件时,加v-if判断后,组件无法被渲染()

avatar
Dec 8th 2020

Version

3.0.4

Reproduction link

https://jsbin.com

Steps to reproduce

<a-directory-tree
                    v-if="treeData"
                    :treeData="treeData"
                    :replaceFields="{
                        children:'children',
                        title:'name',
                        key:'id'
                    }"
                    default-expand-all @select="onSelect" @expand="onExpand">
                </a-directory-tree>

返回数据前treeData为布尔值,利用计算属性处理为数组整个页面无法渲染

What is expected?

异步后台数据返回后渲染组件(v-if)

What is actually happening?

加上v-if判断组件无法渲染

avatar
Dec 8th 2020
avatar
Jan 19th 2021

You should use defineAsyncComponent() declared lazy component.

<template>
  <div id="nav">
    <button @click="sh = !sh">{{ sh }}</button>
    <p v-if="sh">v-if</p>
    <about v-if="sh" />
  </div>
</template>
<script lang="ts">
import { defineAsyncComponent, defineComponent } from "vue";
// lazy component can ne declared there too
//const about = defineAsyncComponent(() => import("@/views/About.vue"));
export default defineComponent({
  components: {
    about: defineAsyncComponent(() => import("@/views/About.vue"))
  },
  data: () => ({ sh: false })
});
</script>