Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信小程序vue3 setup 使用 uni-forms 组件ref获取表单验证validate of null #423

Open
hanxzi opened this issue Dec 16, 2021 · 6 comments

Comments

@hanxzi
Copy link

hanxzi commented Dec 16, 2021

uni-app中使用vue3 setup写法编译到微信小程序表单验证ref获取不到

<template>
  <uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData">
    <uni-forms-item label="姓名" required name="name">
	    <uni-easyinput v-model="valiFormData.name" placeholder="请输入姓名" />
    </uni-forms-item>
	    <button type="primary" size="mini" @click="onSubmit">提交</button>
    </uni-forms>
</ template>
<script>
  import { ref, reactive } from 'vue'
   export default {
       setup(props,context) {
		const valiForm = ref(null)
			const valiFormData = reactive({
					name: '',
			})
			const rules = reactive({
				name: {
					rules: [{
						required: true,
						errorMessage: '姓名不能为空'
					}]
				},
			})
			
			const onSubmit = () => {
				valiForm.value.validate().then(res => {
					console.log("values", valiFormData);
				}).catch(err => {
					console.log('err', err);
				})
			}
			
			return {
				onSubmit,
				rules,
				valiForm,
				valiFormData,
			}
		}
	}
</script>

image

按照vue3官方按照获取ref代码测试

<template> 
  <div ref="root">This is a root element</div>
</template>

<script>
  import { ref, onMounted } from 'vue'

  export default {
    setup() {
      const root = ref(null)

      onMounted(() => {
        // DOM 元素将在初始渲染后分配给 ref
        console.log(root.value) // <div>This is a root element</div>
      })

      return {
        root
      }
    }
  }
</script>

编译在微信小程序里面console.log()输出的内容依旧是null, 在h5测试结果正常

@fxy060608
Copy link

升级最新的 HBuilderX Alpha,或最新的cli

@Anjinc188
Copy link

升级最新的 HBuilderX Alpha,或最新的cli

已经是最新版了,uni-app 真是,感觉是没人维护的 10个组件起码有6个是有问题的

@hanxzi
Copy link
Author

hanxzi commented Sep 6, 2022 via email

@dw08
Copy link

dw08 commented Sep 20, 2022

const valiForm = ref()

@hanxzi
Copy link
Author

hanxzi commented Sep 20, 2022 via email

@pkc918
Copy link

pkc918 commented Nov 2, 2023

解决了嘛

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants