当前位置:   article > 正文

记录uniapp使用uView组件遇到的问题_uniapp使用uview报错

uniapp使用uview报错

先记录一下,后续更新!!!

1、出现Cannot read properties of undefined (reading 'setRules')问题:

a方案:在onReady生命周期调用组件的setRules方法绑定验证规则

  1. // 必须要在onReady生命周期,因为onLoad生命周期组件可能尚未创建完毕
  2. onReady() {
  3. this.$refs.uForm.setRules(this.rules);
  4. }

b方案:如果已经绑定验证规则还是报错,那就检查是不是在页面使用了v-if,把它改为v-show就可以解决了 

c方案:如果你使用的表单是嵌套在popup或者modal里面的时候,这时候表单的元素display为none,所以在onReady里面设置是无效的。应该监听控制表单出现的属性,并利用nextTrick在里面设置规则(亲测有效):

  1. <u-popup :show="approvalShow" mode="center" :round="10" closeOnClickOverlay @close="approvalShow = false">
  2. <view class="approval-popup">
  3. <u--form
  4. labelPosition="left"
  5. :model="approvalForm"
  6. :rules="approvalRules"
  7. ref="approvaForm"
  8. >
  9. ......
  10. </u--form>
  11. </view>
  12. </u-popup>
  1. watch: {
  2. approvalShow(newVal) {
  3. if(newVal){
  4. this.$nextTick(() => {
  5. this.$refs.approvaForm.setRules(this.approvalRules)
  6. });
  7. }
  8. }
  9. },

参考文章:uniapp uview form组件setRules_uview-plus的form表单setrules-CSDN博客

2、this.$refs.uForm.validate不生效

a方案:检查页面是否存在不需要验证规则的prop,把它们去掉再尝试提交,如下在页面prop="password",但rules规则里没有出现password:

  1. <u-form-item label="密码:" label-width="140" prop="password" :border-bottom="false">
  2. <u-input v-model="form.password" type="number" placeholder="请输入密码" border="true" border-color="#4692ef" />
  3. </u-form-item>
  1. rules: {
  2. userName: [{
  3. required: true,
  4. message: '请输入用户名',
  5. trigger: ['change', 'blur'],
  6. }]
  7. }

 b方案:检查使用的uView版本,注意区分:

1.X使用的是:this.$refs.uForm.validate(valid => {})

2.X使用的是:this.$refs.uForm.validate().then(res => {}).catch(errors => {})

3、使用底部导航栏u-tabbar时,运行到微信小程序时选中的tab图片和文字不会高亮(这个错误显得我很傻诶...)

错误代码:因为是自定义的图标但是这里没有配置,而且value并不是使用标签名称name的值匹配的方式

  1. <view class="tabBar_css">
  2. <u-tabbar :value="value" :fixed="true" :placeholder="true" :safeAreaInsetBottom="true" >
  3. <u-tabbar-item v-for="(item,index) in tabBarList" :key="index" :text="item.text"
  4. :name="item.pagePath" @click="click_page(item)">
  5. </u-tabbar-item>
  6. </u-tabbar>
  7. </view>

正确代码: 

  1. <view class="tabBar_css">
  2. <u-tabbar
  3. :value="value"
  4. :fixed="true"
  5. :placeholder="true"
  6. :safeAreaInsetBottom="true"
  7. active-color="#FA5151">
  8. <u-tabbar-item v-for="(item,index) in tabBarList" :key="index" :text="item.text"
  9. @click="click_page(item)">
  10. <image
  11. class="slot-img"
  12. slot="active-icon"
  13. :src="item.selectedIconPath"
  14. ></image>
  15. <image
  16. class="slot-img"
  17. slot="inactive-icon"
  18. :src="item.iconPath"
  19. ></image>
  20. </u-tabbar-item>
  21. </u-tabbar>
  22. </view>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/741395
推荐阅读