模拟数据

一键生成模拟数据方便测试

  1. <!-- 导入需要的包 -->
  2. <script src="https://cdnjs.cloudflare.com/ajax/libs/Mock.js/1.0.0/mock-min.js"></script>

普通用法

模拟数据 - 图1

mock设置true,在列中配置对应的规则即可,当然你可以自己写模拟逻辑,在mock写方法,会返回当前表单的数据,最后return即可,详情参考如下例子

  1. <avue-form :option="option" v-model="form" @submit="handleSubmit"></avue-form>
  2. <script>
  3. const DIC = {
  4. VAILD: [{
  5. label: '真',
  6. value: 'true'
  7. }, {
  8. label: '假',
  9. value: 'false'
  10. }],
  11. SEX: [{
  12. label: '男',
  13. value: 0
  14. }, {
  15. label: '女',
  16. value: 1
  17. }]
  18. }
  19. export default {
  20. data(){
  21. return {
  22. form:{
  23. text:'动态内容1'
  24. },
  25. option:{
  26. mock:true,
  27. submitText: '完成',
  28. column: [{
  29. label: "用户名",
  30. prop: "username",
  31. tip: '这是信息提示',
  32. span: 8,
  33. maxlength: 3,
  34. suffixIcon: 'el-icon-tickets',
  35. prefixIcon: 'el-icon-tickets',
  36. minlength: 2,
  37. mock:{
  38. type:'name',
  39. en:true,
  40. },
  41. rules: [{
  42. required: true,
  43. message: "请输入用户名",
  44. trigger: "blur"
  45. }],
  46. change:({value,column})=>{
  47. this.obj.address=value;
  48. this.$message.success('address change')
  49. },
  50. click:({value,column})=>{
  51. this.$message.success('click')
  52. }
  53. },
  54. {
  55. label: "自定义",
  56. prop: "text2",
  57. mock: (form) => {
  58. return '自定义逻辑' + form.name
  59. },
  60. span:8
  61. },
  62. {
  63. label: "姓名",
  64. prop: "name",
  65. mock:{
  66. type:'name'
  67. },
  68. span:8
  69. },
  70. {
  71. label: "类型",
  72. prop: "type",
  73. type: "select",
  74. dicData: DIC.VAILD,
  75. span:6,
  76. mock:{
  77. type:'dic',
  78. },
  79. },
  80. {
  81. label: "权限",
  82. prop: "grade",
  83. span: 6,
  84. type: "checkbox",
  85. dicData: DIC.VAILD,
  86. mock:{
  87. type:'dic',
  88. },
  89. },
  90. {
  91. label: "开关",
  92. prop: "switch",
  93. span: 6,
  94. type: "switch",
  95. dicData: DIC.SEX,
  96. mock:{
  97. type:'dic'
  98. },
  99. hide: true,
  100. row:true,
  101. },
  102. {
  103. label: "性别",
  104. prop: "sex",
  105. span: 6,
  106. type: "radio",
  107. dicData: DIC.SEX,
  108. mock:{
  109. type:'dic'
  110. },
  111. valueDefault: 0,
  112. change:({value,column})=>{
  113. this.$message.success('change')
  114. }
  115. },
  116. {
  117. label: "数字",
  118. prop: "number",
  119. type: 'number',
  120. span: 6,
  121. precision:2,
  122. mock:{
  123. type:'number',
  124. max:1,
  125. min:2,
  126. precision:2
  127. },
  128. valueDefault: 3,
  129. minRows: 0,
  130. maxRows: 3,
  131. row:true,
  132. },
  133. {
  134. label: "网站",
  135. span: 12,
  136. prop: "url",
  137. prepend:'http://',
  138. mock:{
  139. type:'url',
  140. header:false,
  141. },
  142. append:'com',
  143. row:true,
  144. },
  145. {
  146. label: "日期",
  147. prop: "date",
  148. type: "date",
  149. span:8,
  150. format:'yyyy-MM-dd',
  151. valueFormat:'yyyy-MM-dd',
  152. mock:{
  153. type:'datetime',
  154. format:'yyyy-MM-dd'
  155. },
  156. },
  157. {
  158. label: "日期时间",
  159. prop: "datetime",
  160. type: "datetime",
  161. span:8,
  162. format:'yyyy-MM-dd hh:mm:ss',
  163. valueFormat:'yyyy-MM-dd hh:mm:ss',
  164. mock:{
  165. type:'datetime',
  166. format:'yyyy-MM-dd hh:mm:ss',
  167. now:true,
  168. },
  169. },
  170. {
  171. label: "时间",
  172. prop: "time",
  173. type: "time",
  174. span:8,
  175. format:'hh:mm:ss',
  176. valueFormat:'hh:mm:ss',
  177. mock:{
  178. type:'datetime',
  179. format:'hh:mm:ss'
  180. },
  181. },
  182. {
  183. label: "地址",
  184. span: 24,
  185. prop: "address",
  186. mock:{
  187. type:'county'
  188. },
  189. },{
  190. label: "建议",
  191. span: 24,
  192. prop: "adit",
  193. mock:{
  194. type:'word',
  195. min:10,
  196. max:30
  197. },
  198. }]
  199. }
  200. }
  201. },
  202. methods:{
  203. handleSubmit(form){
  204. this.$message.success(JSON.stringify(this.form))
  205. }
  206. }
  207. }
  208. </script>