u--form.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <uvForm
  3. ref="uForm"
  4. :model="model"
  5. :rules="rules"
  6. :errorType="errorType"
  7. :borderBottom="borderBottom"
  8. :labelPosition="labelPosition"
  9. :labelWidth="labelWidth"
  10. :labelAlign="labelAlign"
  11. :labelStyle="labelStyle"
  12. :customStyle="customStyle"
  13. >
  14. <slot />
  15. </uvForm>
  16. </template>
  17. <script>
  18. /**
  19. * 此组件存在的理由是,在nvue下,u-form被uni-app官方占用了,u-form在nvue中相当于form组件
  20. * 所以在nvue下,取名为u--form,内部其实还是u-form.vue,只不过做一层中转
  21. */
  22. import uvForm from '../u-form/u-form.vue';
  23. import props from '../u-form/props.js'
  24. export default {
  25. // #ifdef MP-WEIXIN
  26. name: 'u-form',
  27. // #endif
  28. // #ifndef MP-WEIXIN
  29. name: 'u--form',
  30. // #endif
  31. mixins: [uni.$u.mpMixin, props, uni.$u.mixin],
  32. components: {
  33. uvForm
  34. },
  35. options: {
  36. styleIsolation: 'shared', // 解除样式隔离
  37. },
  38. created() {
  39. this.children = []
  40. },
  41. methods: {
  42. // 手动设置校验的规则,如果规则中有函数的话,微信小程序中会过滤掉,所以只能手动调用设置规则
  43. setRules(rules) {
  44. this.$refs.uForm.setRules(rules)
  45. },
  46. validate() {
  47. /**
  48. * 在微信小程序中,通过this.$parent拿到的父组件是u--form,而不是其内嵌的u-form
  49. * 导致在u-form组件中,拿不到对应的children数组,从而校验无效,所以这里每次调用u-form组件中的
  50. * 对应方法的时候,在小程序中都先将u--form的children赋值给u-form中的children
  51. */
  52. // #ifdef MP-WEIXIN
  53. this.setMpData()
  54. // #endif
  55. return this.$refs.uForm.validate()
  56. },
  57. validateField(value, callback) {
  58. // #ifdef MP-WEIXIN
  59. this.setMpData()
  60. // #endif
  61. return this.$refs.uForm.validateField(value, callback)
  62. },
  63. resetFields() {
  64. // #ifdef MP-WEIXIN
  65. this.setMpData()
  66. // #endif
  67. return this.$refs.uForm.resetFields()
  68. },
  69. clearValidate(props) {
  70. // #ifdef MP-WEIXIN
  71. this.setMpData()
  72. // #endif
  73. return this.$refs.uForm.clearValidate(props)
  74. },
  75. setMpData() {
  76. this.$refs.uForm.children = this.children
  77. }
  78. },
  79. }
  80. </script>