Vue3通过jsx实现通过ts返回组件

比如我的一个页面有一个位置显示的组件,根据某个值显示不一样的组件,当然可以通过定义一个vue文件,通过一堆if-else显示不一样的组件,但是这样不够灵活,而且很麻烦,那么这里我们就可以通过jsx实现通过ts代码返回不一样的组件

以封装icon图标组件为例,我引入了ant-design-vue的库,如果我们平时用的话,可能要像下边这样

<template>
    <div>
    <a-layout>
        <a-layout-header :style="headerStyle">Header</a-layout-header>
        <a-layout>
           <a-layout-sider>
            <a-menu theme="dark">
              <a-menu-item :key="2">
                <HddOutlined />
                菜单二</a-menu-item>
            </a-menu>
        
           </a-layout-sider>
            <a-layout-content :style="contentStyle">
        <RouterView></RouterView>
     </a-layout-content>
    </a-layout>
    </a-layout>
    </div>
</template>


<script setup lang="ts" name="News">

import  {CopyOutlined,HddOutlined}  from '@ant-design/icons-vue'


</scirpt>

那这样的话就很麻烦,我每次在一个地方使用都要引入对应的icon才可以使用,那么我们可可不可以自己封装一个组件,通过传入不同的名字,显示不一样的组件,见如下语法

import * as icons  from '@ant-design/icons-vue'

interface Module{
    [p:string]:any
}



export default {
   //还需要在这定义标签属性的每个值得类型,注释:类型需要为大写
    props:{
        msg:String
    },
   //这就是标签传过来的值,是一个对象
    setup(props:{msg:string}){
   
        const im:Module=icons
        //标签名称
        let name =im[props.msg];
        return ()=>(<>
        <name></name>
        </>)
        
    }
}

使用如下:

<template>
    <div>
    <a-layout>
        <a-layout-header :style="headerStyle">Header</a-layout-header>
        <a-layout>
           <a-layout-sider>
            <a-menu theme="dark">
              <a-menu-item :key="1">
                <template #icon>
                   //如果自定义的标签只返回一个标签,可以不加,但是如果是一堆标签的话,需要在外层加一个div,否则会出现一个警告(Extraneous non-props attributes (class) were passed to component but could not be automatica)
                  <div>
                   //这里就是自定义的标签组件
                    <Aicon msg="SettingOutlined"/>
                  </div>
                </template>
                菜单一</a-menu-item>
           </a-layout-sider>
            <a-layout-content :style="contentStyle">
        <RouterView></RouterView>
     </a-layout-content>
    </a-layout>
    </a-layout>
    </div>
</template>


<script setup lang="ts" name="News">

import  Aicon   from '@/components/Aicon'

</script>

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您好!对于您的问题,我可以提供以下思路: 1. 使用 xlsx 库解析 base64 编码的 excel 文件,将其转换为 JSON 数据; 2. 使用 vue3 的 template 或 JSX 语法,结合样式库,渲染出表格的 HTML 结构; 3. 针对 excel 文件中的样式,使用 CSS 样式或 JavaScript 动态添加样式,实现表格的样式效果。 具体实现细节,可以参考以下代码示例: ``` <template> <div class="table-container"> <table class="table"> <thead> <tr> <th v-for="(col, index) in columns" :key="index">{{ col }}</th> </tr> </thead> <tbody> <tr v-for="(row, index) in rows" :key="index"> <td v-for="(cell, index) in row" :key="index">{{ cell }}</td> </tr> </tbody> </table> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; import * as XLSX from 'xlsx'; export default defineComponent({ props: { fileData: { type: String, required: true, }, }, data() { return { columns: [], // 表格列名 rows: [], // 表格数据 }; }, mounted() { // 解析 base64 编码的 excel 文件 const file = XLSX.read(this.fileData, { type: 'base64' }); const sheet = file.Sheets[file.SheetNames[0]]; const data = XLSX.utils.sheet_to_json(sheet, { header: 1 }); // 提取表格列名和数据 this.columns = data[0]; this.rows = data.slice(1); }, }); </script> <style scoped> .table-container { overflow-x: auto; } .table { border-collapse: collapse; width: 100%; } .table th, .table td { border: 1px solid #ddd; padding: 8px; text-align: left; } .table th { background-color: #f2f2f2; } </style> ``` 这个组件可以接收一个 base64 编码的 excel 文件,解析出其中的表格数据和样式,并渲染出一个带有原样式的表格。您只需要将该组件引入到您的项目中,并传递正确的参数,即可实现您想要的预览效果。 希望我的回答能对您有所帮助!如果您还有其他问题,欢迎继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EntyIU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值