当前位置: 首页 > news >正文

vnodeToString函数把vnode转为string(innerhtml)

函数

function vnodeToString(vnode) {// 如果是文本节点,直接返回文本内容if (['string', 'boolean', 'undefined', 'null', 'number'].includes(typeof vnode)) {return vnode;}// 转换节点的属性为字符串形式const attrs = Object.keys(vnode.attrs || {}).map((key) => `${key}="${vnode.attrs[key]}"`).join(' ');// 转换子节点为字符串形式const children = (vnode.children || []).map(vnodeToString).join('');// 返回包含标签名、属性和子节点的字符串形式return `<${vnode.tag} ${attrs}>${children}</${vnode.tag}>`;
}

因为业务中需要低代码配置表格,配置文件就用的vnode格式

vue中封装渲染

<template><div v-html="str"></div>
</template><script>
function vnodeToString(vnode) {// 如果是文本节点,直接返回文本内容if (['string', 'boolean', 'undefined', 'null', 'number'].includes(typeof vnode)) {return vnode;}// 转换节点的属性为字符串形式const attrs = Object.keys(vnode.attrs || {}).map((key) => `${key}="${vnode.attrs[key]}"`).join(' ');// 转换子节点为字符串形式const children = (vnode.children || []).map(vnodeToString).join('');// 返回包含标签名、属性和子节点的字符串形式return `<${vnode.tag} ${attrs}>${children}</${vnode.tag}>`;
}
export default {name: "originalTableConfig",props: {config: {type: Object,default: () =>({})}},data(){return {str:'',}},watch: {config:{handler(){this.setStr()},immediate: true,}},methods: {setStr(){this.str = vnodeToString(this.config)},getHtmlStr(){// html前缀 + 样式const htmlPre = `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"></meta><title>Title</title></head><body>`// html后缀const htmlSuf = `</body></html>`// 拼接const res = htmlPre + this.str + htmlSuf// 返回return res},}
}
</script>

使用案例

<template><div><original-table-config :config="myConfig"/></div>
</template><script>
import OriginalTableConfig from "@/views/originalTableConfig/components/originalTableConfig.vue";
// {
//   tag: '',// 元素标签
//   attrs: {}, // 标签属性
//   children: [],// 子元素
// }
export default {components: {OriginalTableConfig},data() {return {myConfig: {}}},mounted() {this.setTable()},methods: {setTable(){const commonTrStyle = "padding: 4px; margin-bottom: 10px;"const commonAttrsLabel = {style: 'text-align: right;' + commonTrStyle}const commonAttrsValue = {style: 'border-bottom: 1px solid black;' + commonTrStyle}this.myConfig = {tag: 'table',children: [{tag: 'tbody',attrs: {style: "width: 685px;"},children: [{tag: 'tr',children: [{tag: "th", attrs: {style: "width: 120px;"}},{tag: "th", attrs: {style: "width: 120px"}},{tag: "th", attrs: {style: "width: 100px"}},{tag: "th", attrs: {style: "width: 120px"}},{tag: "th", attrs: {style: "width: 100px"}},{tag: "th", attrs: {style: "width: 120px"}},]},{tag: 'tr',children: [{tag: 'td',attrs: {style: "font-size: 18px; text-align: center;",colspan: '6'},children: ['撒打发士大夫啥打发大水发收到']},{tag: 'td'},{tag: 'td'},{tag: 'td'},{tag: 'td'},{tag: 'td'}]},{tag: 'tr',children: [{tag: 'td',attrs: {style: "font-size: 18px; text-align: center;padding-bottom: 10px",colspan: '6'},children: ['xxxxxxxxxxx发送到发大水发斯蒂芬表']},{tag: 'td'},{tag: 'td'},{tag: 'td'},{tag: 'td'},{tag: 'td'}]},{tag: 'tr',children: [{tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},{tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue},{tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},{tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue},{tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},{tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue}]},{tag: 'tr',children: [{tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},{tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue},{tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},{tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue},{tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},{tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue}]},{tag: 'tr',children: [{tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},{tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue},{tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},{tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue},{tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},{tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue}]},{tag: 'tr',children: [{tag: 'td', children: ['xxx负责人:'], attrs: commonAttrsLabel},{tag: 'td', children: [''], attrs: commonAttrsValue},{tag: 'td', children: ['xxx领导:'], attrs: commonAttrsLabel},{tag: 'td', children: [''], attrs: commonAttrsValue},{tag: 'td', children: ['xxx人:'], attrs: commonAttrsLabel},{tag: 'td', children: [''], attrs: commonAttrsValue}]},{tag: 'tr',children: [{tag: 'td', children: ['xxx部门:'], attrs: commonAttrsLabel},{tag: 'td', children: [''], attrs: commonAttrsValue},{tag: 'td'},{tag: 'td'},{tag: 'td', children: ['xxx单位:'], attrs: commonAttrsLabel},{tag: 'td', children: [''], attrs: commonAttrsValue}]},{tag: 'tr',children: [{tag: 'td', children: ['xxx日期:'], attrs: commonAttrsLabel},{tag: 'td', children: [''], attrs: commonAttrsValue},{tag: 'td', children: ['xxx日期:'], attrs: commonAttrsLabel},{tag: 'td', children: [''], attrs: commonAttrsValue},{tag: 'td', children: ['xxx日期:'], attrs: commonAttrsLabel},{tag: 'td', children: [''], attrs: commonAttrsValue}]},]}]}}}
}
</script>

相关文章:

vnodeToString函数把vnode转为string(innerhtml)

函数 function vnodeToString(vnode) {// 如果是文本节点&#xff0c;直接返回文本内容if ([string, boolean, undefined, null, number].includes(typeof vnode)) {return vnode;}// 转换节点的属性为字符串形式const attrs Object.keys(vnode.attrs || {}).map((key) > …...

【Halcon】C# HTuple多参数设置小技巧

比如&#xff0c;在halcon中我们经常这么写&#xff1a; dev_disp_text (hello, window, 100, 200, red, [box,shadow],[true,false])[‘box’,‘shadow’] 和 [‘true’,‘false’] 成对出现。 可以同时对多个参数设置。 如果用halcon翻译C#&#xff0c;你会得到&#xff1a…...

此芯科技加入绿色计算产业联盟,参编绿色计算产业发展白皮书

近日&#xff0c;此芯科技正式加入绿色计算产业联盟&#xff08;Green Computing Consortium&#xff0c;简称GCC&#xff09;&#xff0c;以Arm架构通用智能CPU芯片及高能效的Arm PC计算解决方案加速构建软硬协同的绿色计算生态体系&#xff0c;推动绿色计算产业加速发展。 继…...

webrtc 生成unpack_aecdump工具

1.download webrtc-code https://git.ringcentral.com/build/webrtc-build 2.下载webrtc代码 3.terminal 进入src目录下 4.构建目录&#xff1a; terminal执行&#xff1a;gn gen out/Release --argsis_component_buildfalse 5.构建可执行文件&#xff1a; terminal执行…...

数据结构第四课 -----线性表之队列

作者前言 &#x1f382; ✨✨✨✨✨✨&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f382; ​&#x1f382; 作者介绍&#xff1a; &#x1f382;&#x1f382; &#x1f382; &#x1f389;&#x1f389;&#x1f389…...

蓝桥杯 第 3 场算法双周赛4,7题

迷宫逃脱 一眼数字三角形模型&#xff0c;因为是要求最大值&#xff0c;而且对转移状态有限制&#xff0c;所以需要注意dp状态的初始化&#xff0c;可以将所有状态赋值为-0x7f&#xff0c;然后将dp[0][1]和dp[1][0]初始化为0&#xff0c;又因为考虑到起始点a[1][1]&#xff0c…...

西安有哪些比较好的设计院?西安名企设计院介绍!

1、西北综合勘察设计研究院&#xff08;地址&#xff1a;陕西省西安市习武园9号&#xff09; 西北综合勘察设计研究院始建于1952年&#xff0c;是西北地区建设领域成立最早、规模最大的的综合性甲级勘察设计咨询科研单位&#xff0c;公司业务以工程勘察、建筑设计、市政设计、…...

Java获取Jar、War包路径,并生成可编辑修改的本地配置文件

前言 本地的可修改配置文件的编写理应是一个很常用的功能&#xff0c;但由于数据库的存在&#xff0c;它鲜少被提及&#xff0c;大多数我们直接存储到数据库中了。 以至于现今&#xff0c;除了没接触数据库的新手时常使用它以外&#xff0c;它没有太多的出场机会。 也因此&am…...

FPGA UDP RGMII 千兆以太网(4)ARP ICMP UDP

1 以太网帧 1.1 1以太网帧格式 下图为以太网的帧格式: 前导码(Preamble):8 字节,连续 7 个 8’h55 加 1 个 8’hd5,表示一个帧的开始,用于双方 设备数据的同步。 目的 MAC 地址:6 字节,存放目的设备的物理地址,即 MAC 地址 源 MAC 地址:6 字节,存放发送端设备的…...

【视觉SLAM十四讲学习笔记】第二讲——初识SLAM

专栏系列文章如下&#xff1a; 【视觉SLAM十四讲学习笔记】第一讲 一个机器人&#xff0c;如果想要探索某一块区域&#xff0c;它至少需要知道两件事&#xff1a; 我在什么地方——定位周围环境是什么样——建图 一方面需要明白自身的状态&#xff08;即位置&#xff09;&#…...

Python交易-通过Financial Modeling Prep (FMP)选择行业

介绍 在您的交易旅程中,无论您是在寻找理想的股票、板块还是指标,做出明智的决策对于您的成功至关重要。然而,收集和分析所需的大量数据可能相当艰巨。财务建模准备 (FMP) API的...

AI创作系统ChatGPT网站源码+详细搭建部署教程+支持DALL-E3文生图/支持最新GPT-4-Turbo-With-Vision-128K多模态模型

一、AI创作系统 SparkAi创作系统是基于OpenAI很火的ChatGPT进行开发的Ai智能问答系统和Midjourney绘画系统&#xff0c;支持OpenAI-GPT全模型国内AI全模型。本期针对源码系统整体测试下来非常完美&#xff0c;可以说SparkAi是目前国内一款的ChatGPT对接OpenAI软件系统。那么如…...

快速生成力扣链表题的链表,实现快速调试

关于力扣链表题需要本地调试创建链表的情况 我们在练习链表题&#xff0c;力扣官方需要会员&#xff0c;我们又不想开会员&#xff0c;想在本地调试给你们提供的代码 声明&#xff1a;本人也是参考的别人的代码&#xff0c;给你们提供不同语言生成链表 参考链接&#xff1a; 参…...

threejs(13)-着色器设置点材质

着色器材质内置变量 three.js着色器的内置变量&#xff0c;分别是 gl_PointSize&#xff1a;在点渲染模式中&#xff0c;控制方形点区域渲染像素大小&#xff08;注意这里是像素大小&#xff0c;而不是three.js单位&#xff0c;因此在移动相机是&#xff0c;所看到该点在屏幕…...

计算机网络专栏 学习导航or使用说明

计算机网络各章笔记 计算机网络_第一章_计算机网络的概述 计算机网络_第二章_物理层 计算机网络_第三章_数据链路层 计算机网络_第四章网络层_网络层概述_网际协议IP 计算机网络各章习题 计算机网络第一章习题_网络概述 计算机网络第二章习题_物理层 计算机网络第三章习…...

git clone:SSL: no alternative certificate subject name matches target host name

git clone 时的常见错误&#xff1a; fatal: unable to access ‘https://ip_or_domain/xx/xx.git/’: SSL: no alternative certificate subject name matches target host name ‘ip_or_domain’ 解决办法&#xff1a; disable ssl verify git config --global http.sslVe…...

代码随想录图论|130. 被围绕的区域 417太平洋大西洋水流问题

130. 被围绕的区域 **题目&#xff1a;**给你一个 m x n 的矩阵 board &#xff0c;由若干字符 ‘X’ 和 ‘O’ &#xff0c;找到所有被 ‘X’ 围绕的区域&#xff0c;并将这些区域里所有的 ‘O’ 用 ‘X’ 填充。 题目链接&#xff1a;130. 被围绕的区域 解题思路&#xff1a…...

Outlook无法显示阅读窗格

Outlook无法显示阅读窗格 故障现象 Outlook主界面不显示阅读窗格 故障截图 故障原因 阅读窗格被关闭 解决方案 1、打开Outlook - 视图 – 阅读窗格 2、选择“靠右”或者“底部”&#xff0c;正常显示阅读窗格...

tensorflow 1.15 gpu docker环境搭建;Nvidia Docker容器基于TensorFlow1.15测试GPU;——全流程应用指南

前言: TensorFlow简介 TensorFlow 在新款 NVIDIA Pascal GPU 上的运行速度可提升高达 50%&#xff0c;并且能够顺利跨 GPU 进行扩展。 如今&#xff0c;训练模型的时间可以从几天缩短到几小时 TensorFlow 使用优化的 C 和 NVIDIA CUDA 工具包编写&#xff0c;使模型能够在训练…...

一个22届被裁前端思想上得转变

距离上篇文章已经过去了三个多月&#xff0c;这个三个月&#xff0c;经历了技术攻坚&#xff0c;然后裁员&#xff0c;退房&#xff0c;回老家&#xff0c;找工作。短短的几个月&#xff0c;就经历社会的一次次毒打&#xff0c;特别是找工作&#xff0c;虽然算上实习我也有两年…...

车载Java OTA升级崩溃率从18.7%降至0.3%:基于Delta Patch + 类隔离热修复的4步标准化流程

第一章&#xff1a;车载Java OTA升级崩溃率从18.7%降至0.3%&#xff1a;基于Delta Patch 类隔离热修复的4步标准化流程在车载嵌入式Java环境&#xff08;JVM 11&#xff0c;ART兼容层&#xff09;中&#xff0c;OTA升级引发的ClassCastException与NoClassDefFoundError曾导致高…...

智能转换驱动科研效率:DeTikZify重构学术图表自动化新范式

智能转换驱动科研效率&#xff1a;DeTikZify重构学术图表自动化新范式 【免费下载链接】DeTikZify Synthesizing Graphics Programs for Scientific Figures and Sketches with TikZ 项目地址: https://gitcode.com/gh_mirrors/de/DeTikZify 在科研成果可视化的关键环节…...

雪花算法替代MurmurHash后的提升(短链接项目中的唯一性设计)

短链接服务的核心功能&#xff0c;是将一个长网址&#xff08;比如几百个字符的 URL&#xff09;转换成一个短码&#xff0c;用户访问短码时&#xff0c;服务端会将其重定向回原始的长链接。 考虑到快速生成&#xff08;防止高并发下&#xff0c;性能变差&#xff09;和长变短的…...

3步解锁显卡潜力:OptiScaler跨平台开源上采样技术配置攻略

3步解锁显卡潜力&#xff1a;OptiScaler跨平台开源上采样技术配置攻略 【免费下载链接】OptiScaler OptiScaler bridges upscaling/frame gen across GPUs. Supports DLSS2/XeSS/FSR2 inputs, replaces native upscalers, enables FSR3 FG on non-FG titles. Supports Nukem mo…...

像素史诗智识终端效果展示:自动提取数据关键指标并生成结论段落

像素史诗智识终端效果展示&#xff1a;自动提取数据关键指标并生成结论段落 1. 产品概览&#xff1a;当科研遇上像素冒险 像素史诗智识终端(Pixel Epic Wisdom Terminal)是一款颠覆传统的研究报告辅助工具。它将枯燥的数据分析过程转化为一场充满像素美学的RPG冒险&#xff0…...

FPGA时序约束实战:Set_Clock_Sense的精准控制与路径优化

1. 为什么需要Set_Clock_Sense约束 在FPGA设计中&#xff0c;时钟网络就像城市交通系统中的红绿灯&#xff0c;控制着数据在各个寄存器之间的流动节奏。但实际工程中经常会遇到一些特殊场景&#xff1a;比如一个多路选择器&#xff08;MUX&#xff09;同时接收多个时钟源&#…...

告别HEIC预览盲区:让Windows用户轻松驾驭苹果图像格式

告别HEIC预览盲区&#xff1a;让Windows用户轻松驾驭苹果图像格式 【免费下载链接】windows-heic-thumbnails Enable Windows Explorer to display thumbnails for HEIC files 项目地址: https://gitcode.com/gh_mirrors/wi/windows-heic-thumbnails 问题场景&#xff1…...

PROJECT MOGFACE自动化办公助手:集成Python脚本处理Excel与生成报告

PROJECT MOGFACE自动化办公助手&#xff1a;告别重复劳动&#xff0c;让报告自己“写”自己 你是不是也受够了每周、每月那些格式固定的数据报告&#xff1f;从一堆Excel表格里复制粘贴数据&#xff0c;再绞尽脑汁组织语言&#xff0c;最后排版成一份像样的文档。这个过程枯燥…...

实战react项目:基于快马ai快速构建包含图表与导航的用户数据仪表盘

最近在做一个用户数据仪表盘项目&#xff0c;正好用React配合Ant Design实现了一套完整的界面。这种包含导航、图表和动态数据的页面在后台系统中很常见&#xff0c;记录下我的实现思路和踩坑经验。 项目结构规划 首先用create-react-app初始化项目&#xff0c;然后按功能模块…...

G-Helper终极指南:释放华硕笔记本全部潜力的轻量级控制工具

G-Helper终极指南&#xff1a;释放华硕笔记本全部潜力的轻量级控制工具 【免费下载链接】g-helper Lightweight, open-source control tool for ASUS laptops and ROG Ally. Manage performance modes, fans, GPU, battery, and RGB lighting across Zephyrus, Flow, TUF, Stri…...