codeModal.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!--
  2. * @Description: 代码预览组件
  3. * @Author: kcz
  4. * @Date: 2019-12-30 00:37:05
  5. * @LastEditors: kcz
  6. * @LastEditTime: 2021-05-27 15:18:39
  7. -->
  8. <template>
  9. <a-modal
  10. title="代码"
  11. :footer="null"
  12. :visible="visible"
  13. @cancel="handleCancel"
  14. wrapClassName="code-modal-9136076486841527"
  15. style="top:20px;"
  16. width="850px"
  17. :destroyOnClose="true"
  18. >
  19. <a-tabs tabPosition="left" style="height:100%">
  20. <a-tab-pane tab="VUE" key="1">
  21. <!-- vue code start -->
  22. <previewCode :editorJson="editorVueJson" fileFormat="vue" />
  23. <!-- vue code end -->
  24. </a-tab-pane>
  25. <a-tab-pane tab="HTML" key="2">
  26. <!-- html code start -->
  27. <previewCode :editorJson="editorHtmlJson" fileFormat="html" />
  28. <!-- html code end -->
  29. </a-tab-pane>
  30. </a-tabs>
  31. </a-modal>
  32. </template>
  33. <script>
  34. const codeVueFront = `<template>
  35. <div>
  36. <k-form-build
  37. :value="jsonData"
  38. ref="KFB"
  39. @submit="handleSubmit"
  40. />
  41. <button @click="getData">提交</button>
  42. </div>
  43. </template>
  44. <script>
  45. export default {
  46. name: 'Demo',
  47. data () {
  48. return {
  49. jsonData: `;
  50. /* eslint-disable */
  51. let codeVueLast = `
  52. }
  53. },
  54. methods: {
  55. handleSubmit(p) {
  56. // 通过表单提交按钮触发,获取promise对象
  57. p().then(res => {
  58. // 获取数据成功
  59. alert(JSON.stringify(res))
  60. })
  61. .catch(err => {
  62. console.log(err, '校验失败')
  63. })
  64. },
  65. getData() {
  66. // 通过函数获取数据
  67. this.$refs.KFB.getData().then(res => {
  68. // 获取数据成功
  69. alert(JSON.stringify(res))
  70. })
  71. .catch(err => {
  72. console.log(err, '校验失败')
  73. })
  74. }
  75. }
  76. }
  77. <\/script>`;
  78. let codeHtmlFront = `<!DOCTYPE html>
  79. <html>
  80. <head>
  81. <title>表单设计器kcz</title>
  82. <meta charset="UTF-8">
  83. <link rel="stylesheet" href="http://unpkg.com/k-form-design/lib/k-form-design.css">
  84. </head>
  85. <body>
  86. <div class="app">
  87. <k-form-build ref="KFB" @submit="handleSubmit" :value="jsonData"></k-form-build>
  88. <button @click="getData">提交</button>
  89. </div>
  90. <script src="http://cdn.kcz66.com/vue.min.js"><\/script>
  91. <script src="http://unpkg.com/k-form-design/lib/k-form-design.umd.min.js"><\/script>
  92. <script>
  93. let jsonData = `;
  94. let codeHtmlLast = `
  95. let vm = new Vue({
  96. el: '.app',
  97. data: {
  98. jsonData
  99. },
  100. methods: {
  101. handleSubmit(p) {
  102. // 通过表单提交按钮触发,获取promise对象
  103. p().then(res => {
  104. // 获取数据成功
  105. alert(JSON.stringify(res))
  106. })
  107. .catch(err => {
  108. console.log(err, '校验失败')
  109. })
  110. },
  111. getData() {
  112. // 通过函数获取数据
  113. this.$refs.KFB.getData().then(res => {
  114. // 获取数据成功
  115. alert(JSON.stringify(res))
  116. })
  117. .catch(err => {
  118. console.log(err, '校验失败')
  119. })
  120. }
  121. }
  122. })
  123. <\/script>
  124. </body>
  125. </html>`
  126. /* eslint-enable */
  127. import previewCode from "../../PreviewCode/index";
  128. export default {
  129. name: "CodeModal",
  130. data() {
  131. return {
  132. visible: false,
  133. editorVueJson: "",
  134. editorHtmlJson: "",
  135. jsonData: {}
  136. };
  137. },
  138. watch: {
  139. visible(val) {
  140. if (val) {
  141. this.editorVueJson =
  142. codeVueFront + JSON.stringify(this.jsonData) + codeVueLast;
  143. this.editorHtmlJson =
  144. codeHtmlFront + JSON.stringify(this.jsonData) + codeHtmlLast;
  145. }
  146. }
  147. },
  148. components: {
  149. previewCode
  150. },
  151. methods: {
  152. handleCancel() {
  153. this.visible = false;
  154. }
  155. }
  156. };
  157. </script>