vue中let that=this的作用

this 会随着上下文环境而变换它的指向,在当前作用域中设置一个变量用来存储 this 可以防止在其他地方找不到 this 的错误。


```javascript
$("#btn").click(function(){
    var that = this;//这里this和that都代表了"#btn"这个对象
    $(".tr").each(function(){
          this;//在这里this代表的是每个遍历到的".tr"对象
          that;//仍代表"#btn"对象
    })
})
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当把两段代码合并到 HBuilderX 的 Vue 项目时,可以按照以下方式进行修改: ```vue <template> <view> <textarea v-model="textVal"></textarea> <input v-model="contact" type="text" placeholder="联系方式" /> <button @click="handleFormSubmit">提交</button> </view> </template> <script> export default { data() { return { textVal: '', contact: '' }; }, methods: { handleFormSubmit() { const { textVal, contact } = this; const regPhone = /^1[3578]\d{9}$/; const regEmail = /^[a-z\d_.-]+@[a-z\d_-]+\.[a-z\d_-]+$/i; // 进行合法性验证 if (!textVal.trim()) { uni.showToast({ title: '输入不合法', icon: 'none', mask: true }); return; } if (!regPhone.test(contact) && !regEmail.test(contact)) { uni.showToast({ title: '联系方式不合法', icon: 'none', mask: true }); return; } // 其他处理逻辑 } } }; </script> ``` 在上述代码,将 `this.data` 的 `textVal` 和 `chooseImgs` 分别替换为 Vue 的 `textVal` 和 `chooseImgs`(这里没有提到 `chooseImgs`,因此省略了)。将 `let _that = this;` 替换为 Vue 的 `this`。将 `e.detail.value.opinion` 替换为 Vue 的双向绑定 `v-model` 的 `textVal`,将 `e.detail.value.contact` 替换为 Vue 的双向绑定 `v-model` 的 `contact`。 然后,将两段代码合并到一起,在 `handleFormSubmit` 方法直接使用 `textVal` 和 `contact` 的值进行处理和验证。 请注意,以上代码只是一个示例,实际使用时可能需要根据具体需求进行适当的修改和调整。同时,如果你在 HBuilderX 使用了 uView 或其他 UI 框架,可能还需要根据具体框架的文档进行相应的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值