从前有匹马叫代码
心若没有栖息的地方,到哪里都是流浪

本文主要展示一下如何给异步组件进行参数传递:

通过 h 函数就可以啦

versions:

  • vue@3.2.13

 

子组件 Async.vue

<template>
    <div>
        <span>异步注册组件:</span>
        <span>Props:{{ message }}</span>
    </div>
</template>

<script setup>
const props = defineProps({
    message: String,
});
</script>

父组件 App.vue

<template>
    <div>Hello world</div>
    <AsyncComponent></AsyncComponent>
</template>

<script setup>
import { defineAsyncComponent, h } from "vue";
import Async from "./components/Async.vue";
import Loading from "./components/Loading.vue";

const AsyncComponent = defineAsyncComponent({
    loader: () => {
        return new Promise((resolve) => {
            setTimeout(() => {
                const WithPropsAsync = h(Async, { message: "嘿嘿" });
                resolve(WithPropsAsync);
            }, 3000);
        });
    },
    loadingComponent: Loading,
});
</script>

 

posted on 2022-10-25 10:53  从前有匹马叫代码  阅读(285)  评论(0编辑  收藏  举报