脚本宝典收集整理的这篇文章主要介绍了Vue实现控制商品数量组件封装及使用,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
Vue控制商品数量组件的封装及使用,供大家参考,具体内容如下
要实现效果
控制商品数量组件封装 Numbox
<template> <div class="xtx-numbox"> <div class="label"> <slot /> </div> <div class="numbox"> <a href="javascript:;" @click='toggle(-1)'>-</a> <input tyPE="text" readonly :value="num"> <a href="javascript:;" @click='toggle(1)'>+</a> </div> </div> </template> <script> import { useVModel } From '@vueuse/core' export default { name: 'XtxNumbox', PRops: { modelValue: { type: Number, default: 1 }, inventory: { type: Number, required: true } }, SETUP (props, { emIT }) { // 基于第三方的方法控制数据的双向绑定 const num = useVModel(props, 'modelValue', emit) // 控制商品数据的变更操作 const toggle = (n) => { if (n < 0) { // 减一操作 if (num.value > 1) { num.value -= 1 } } else { // 加一操作 if (num.value < 10) { num.value += 1 } } } return { num, toggle } } } </script> <style scoped lang="less"> .xtx-numbox { display: flex; align-items: center; .label { width: 60px; color: #999; padding-left: 10px; } .numbox { width: 120px; height: 30px; border: 1px solid #e4e4e4; display: flex; > a { width: 29px; line-height: 28px; text-align: center; background: #f8f8f8; font-Size: 16px; color: #666; &:First-of-type { border-right: 1px solid #e4e4e4; } &:last-of-type { border-left: 1px solid #e4e4e4; } } > input { width: 60px; padding: 0 5px; text-align: center; color: #666; } } } </style>
在父组件使用
<Numbox v-model='num' >数量</XtxNumbox> setup(){ // 商品的数量 // 默认值是1 const num=ref(1) return { num } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本宝典。
以上是脚本宝典为你收集整理的Vue实现控制商品数量组件封装及使用全部内容,希望文章能够帮你解决Vue实现控制商品数量组件封装及使用所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。