• vnodeToString函数把vnode转为string(innerhtml)


    函数

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

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

    vue中封装渲染

    1. <template>
    2. <div v-html="str">div>
    3. template>
    4. <script>
    5. function vnodeToString(vnode) {
    6. // 如果是文本节点,直接返回文本内容
    7. if (['string', 'boolean', 'undefined', 'null', 'number'].includes(typeof vnode)) {
    8. return vnode;
    9. }
    10. // 转换节点的属性为字符串形式
    11. const attrs = Object.keys(vnode.attrs || {})
    12. .map((key) => `${key}="${vnode.attrs[key]}"`)
    13. .join(' ');
    14. // 转换子节点为字符串形式
    15. const children = (vnode.children || [])
    16. .map(vnodeToString)
    17. .join('');
    18. // 返回包含标签名、属性和子节点的字符串形式
    19. return `<${vnode.tag} ${attrs}>${children}${vnode.tag}>`;
    20. }
    21. export default {
    22. name: "originalTableConfig",
    23. props: {
    24. config: {
    25. type: Object,
    26. default: () =>({})
    27. }
    28. },
    29. data(){
    30. return {
    31. str:'',
    32. }
    33. },
    34. watch: {
    35. config:{
    36. handler(){
    37. this.setStr()
    38. },
    39. immediate: true,
    40. }
    41. },
    42. methods: {
    43. setStr(){
    44. this.str = vnodeToString(this.config)
    45. },
    46. getHtmlStr(){
    47. // html前缀 + 样式
    48. const htmlPre = `Title`
    49. // html后缀
    50. const htmlSuf = ``
    51. // 拼接
    52. const res = htmlPre + this.str + htmlSuf
    53. // 返回
    54. return res
    55. },
    56. }
    57. }
    58. script>

    使用案例

    1. <script>
    2. import OriginalTableConfig from "@/views/originalTableConfig/components/originalTableConfig.vue";
    3. // {
    4. // tag: '',// 元素标签
    5. // attrs: {}, // 标签属性
    6. // children: [],// 子元素
    7. // }
    8. export default {
    9. components: {OriginalTableConfig},
    10. data() {
    11. return {
    12. myConfig: {}
    13. }
    14. },
    15. mounted() {
    16. this.setTable()
    17. },
    18. methods: {
    19. setTable(){
    20. const commonTrStyle = "padding: 4px; margin-bottom: 10px;"
    21. const commonAttrsLabel = {style: 'text-align: right;' + commonTrStyle}
    22. const commonAttrsValue = {style: 'border-bottom: 1px solid black;' + commonTrStyle}
    23. this.myConfig = {
    24. tag: 'table',
    25. children: [
    26. {
    27. tag: 'tbody',
    28. attrs: {
    29. style: "width: 685px;"
    30. },
    31. children: [
    32. {
    33. tag: 'tr',
    34. children: [
    35. {tag: "th", attrs: {style: "width: 120px;"}},
    36. {tag: "th", attrs: {style: "width: 120px"}},
    37. {tag: "th", attrs: {style: "width: 100px"}},
    38. {tag: "th", attrs: {style: "width: 120px"}},
    39. {tag: "th", attrs: {style: "width: 100px"}},
    40. {tag: "th", attrs: {style: "width: 120px"}},
    41. ]
    42. },
    43. {
    44. tag: 'tr',
    45. children: [
    46. {
    47. tag: 'td',
    48. attrs: {
    49. style: "font-size: 18px; text-align: center;",
    50. colspan: '6'
    51. },
    52. children: ['撒打发士大夫啥打发大水发收到']
    53. },
    54. {tag: 'td'},
    55. {tag: 'td'},
    56. {tag: 'td'},
    57. {tag: 'td'},
    58. {tag: 'td'}
    59. ]
    60. },
    61. {
    62. tag: 'tr',
    63. children: [
    64. {
    65. tag: 'td',
    66. attrs: {
    67. style: "font-size: 18px; text-align: center;padding-bottom: 10px",
    68. colspan: '6'
    69. },
    70. children: ['xxxxxxxxxxx发送到发大水发斯蒂芬表']
    71. },
    72. {tag: 'td'},
    73. {tag: 'td'},
    74. {tag: 'td'},
    75. {tag: 'td'},
    76. {tag: 'td'}
    77. ]
    78. },
    79. {
    80. tag: 'tr',
    81. children: [
    82. {tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},
    83. {tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue},
    84. {tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},
    85. {tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue},
    86. {tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},
    87. {tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue}
    88. ]
    89. },
    90. {
    91. tag: 'tr',
    92. children: [
    93. {tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},
    94. {tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue},
    95. {tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},
    96. {tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue},
    97. {tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},
    98. {tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue}
    99. ]
    100. },
    101. {
    102. tag: 'tr',
    103. children: [
    104. {tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},
    105. {tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue},
    106. {tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},
    107. {tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue},
    108. {tag: 'td', children: ['xxx金额:'], attrs: commonAttrsLabel},
    109. {tag: 'td', children: ['xxxxxxxxx', '元'], attrs: commonAttrsValue}
    110. ]
    111. },
    112. {
    113. tag: 'tr',
    114. children: [
    115. {tag: 'td', children: ['xxx负责人:'], attrs: commonAttrsLabel},
    116. {tag: 'td', children: [''], attrs: commonAttrsValue},
    117. {tag: 'td', children: ['xxx领导:'], attrs: commonAttrsLabel},
    118. {tag: 'td', children: [''], attrs: commonAttrsValue},
    119. {tag: 'td', children: ['xxx人:'], attrs: commonAttrsLabel},
    120. {tag: 'td', children: [''], attrs: commonAttrsValue}
    121. ]
    122. },
    123. {
    124. tag: 'tr',
    125. children: [
    126. {tag: 'td', children: ['xxx部门:'], attrs: commonAttrsLabel},
    127. {tag: 'td', children: [''], attrs: commonAttrsValue},
    128. {tag: 'td'},
    129. {tag: 'td'},
    130. {tag: 'td', children: ['xxx单位:'], attrs: commonAttrsLabel},
    131. {tag: 'td', children: [''], attrs: commonAttrsValue}
    132. ]
    133. },
    134. {
    135. tag: 'tr',
    136. children: [
    137. {tag: 'td', children: ['xxx日期:'], attrs: commonAttrsLabel},
    138. {tag: 'td', children: [''], attrs: commonAttrsValue},
    139. {tag: 'td', children: ['xxx日期:'], attrs: commonAttrsLabel},
    140. {tag: 'td', children: [''], attrs: commonAttrsValue},
    141. {tag: 'td', children: ['xxx日期:'], attrs: commonAttrsLabel},
    142. {tag: 'td', children: [''], attrs: commonAttrsValue}
    143. ]
    144. },
    145. ]
    146. }
    147. ]
    148. }
    149. }
    150. }
    151. }
    152. script>

  • 相关阅读:
    在建筑设计方面3DMax和Maya哪一个更好?
    一台电脑生成两个ssh,绑定两个GitHub账号
    深入解析Kafka消息丢失的原因与解决方案
    LeetCode - Easy - 509. Fibonacci Number
    并发编程之Executor线程池原理与源码解读
    QtC++与QColumnView详解
    喜报 | 博睿数据两项发明专利获得国家知识产权局授权,累计发明专利11项
    程序地址空间
    SpringBoot接收参数的几种常用方式
    从gles,vulkan到metal(二)-- 同步和内存
  • 原文地址:https://blog.csdn.net/qq_30306717/article/details/134438094