使用import(path).then()异步加载虚拟DOM

2021-04-10  本文已影响0人  曾祥辉

直接上代码

//父组件
<template>
  <Componet path="@/componet/Componet.vue ">
  </Componet >
</template>
.......
//子组件
<template>
  <article v-html="content">
  </article>
</template>
<script lang="ts">
import { ref } from "vue";
export default {
  props: {
    path: {
      type: String,
      required: true,
    },
  },
  setup(props) {
    const content = ref<string>(null);
    // 异步引入组件
    import(props.path).then((res) => {
      // 使用console.log({...res})打印发现虚拟节点在default
      content.value = res.default;
    });
    return {
      content,
    };
  },
};
</script>
上一篇 下一篇

猜你喜欢

热点阅读