Vue开发学习笔记:使用h()动态渲染ant-design-vue组件

<script setup>
import { h, createApp, onMounted, render } from "vue";
import Antd, { Button, Select, Row, Col } from "ant-design-vue"; // 假设这是Vue 3的兼容版本

// 使用h函数来渲染ant-design-vue的按钮
const renderButton = () => h(Button, { type: "primary" }, "Click me!");
const renderArow = () => h(Row, {}, createCol);
const createCol = () => {
  const colArray = [];
  for (let index = 0; index < 6; index++) {
    colArray.push(h(Col, { span: 4 }, "测试" + index));
  }
  return colArray;
};
onMounted(() => {
  const parentComponent = document.getElementById("renderCustom");
  console.log("🚀 ~ parentComponent:", parentComponent);
  const app = createApp({
    render: renderArow,
  }); // 创建一个临时的 Vue 应用
  app.use(Antd); // 由于临时创建了一个Vue应用因此需要重新引入组件
  app.mount(parentComponent); // 将子组件挂载到目标元素
});
</script>

<!-- 如果你在模板中直接使用 -->
<template>
  <div id="renderCustom"></div>
  <a-row>
    <a-col :span="4">1</a-col>
    <a-col :span="4">2</a-col>
    <a-col :span="4">3</a-col>
    <a-col :span="4">4</a-col>
    <a-col :span="4">5</a-col>
    <a-col :span="4">6</a-col>
  </a-row>
</template>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值