一个网站的上线流程,推广咨询,西安做网站公司 玖佰网络,厦门seo优化多少钱特殊说明#xff1a; 以下vue3语法是基于 3.4之前版本进行使用的#xff0c;3.4之后的版本 引入了 defineModel 宏#xff0c;后续会介绍defineModel
1、vue3 与vue2 中v-model区别 vue3 中v-model绑定的不再是value#xff0c;而是modelValue#xff0c;接收的方法也不再…特殊说明 以下vue3语法是基于 3.4之前版本进行使用的3.4之后的版本 引入了 defineModel 宏后续会介绍defineModel
1、vue3 与vue2 中v-model区别 vue3 中v-model绑定的不再是value而是modelValue接收的方法也不再是input而是update:modelValue vue2 中v-model的主要原因是由于value和input事件可能另有它用比如select、textareaselect选择框绑定的是checked,而不是value同样接受事件也不是input而是change 如下图
2、v3 v2 中绑定单个值
2.1 vue3中双向绑定单个值以及自定义绑定值时候 v-model 在原生input上使用 父组件
templateinput v-modelmyV3Vale /!-- 编译之后等价于 --input :valuemyV3Vale inputmyV3Vale $event.target.value/!-- 引用组件中 // 默认传值是 modelVale--my-son-com :modelValuemyV3Vale :otherPropFieldotherPropFieldupdate:modelValuenewValue myV3Vale newValueupdate:otherPropFieldnewValue myV3Vale newValue/my-son-com
/template子组件
// 以下为MySonCom.vue
templateinput:valueprops.modelValueinputemit(update:modelValue, $event.target.value)/
/template
script setup
// 默认传参必须是modelValue事件必须是update:modelValue在子组件中如下
const props defineProps({modelValue: String
})
const emits defineEmits([update:modelValue])
const update (e) {emits(update:modelValue, e.target.value)
}
//多个参数时 传递额外参数 otherPropField 必须是如下v-model:otherPropField,对应的事件是update:otherPropField
const otherProp defineProps({otherPropField: String
})
const othersEmits defineEmits([update:otherPropField])
const otherUpdate (e) {emits(update:otherPropField, e.target.value)
}
/script2.2、v2 中绑定单个值、多个值 父组件
template!-- 默认传值是 value --my-com-v2 v-modelmyV2Value v-bind:otherField.syncotherField/// 等价与my-com-v2 :valuemyV2Value inputmyV2Value $event/// 绑定额外参数 使用 .syncmy-com-v2 v-bind:otherField.syncotherField/my-com-v2 v-bind:otherFieldotherField v-on:update:otherFieldotherField $event/!-- 在组件中 --
/template子组件 MyComV2.vue
templateinput typetext :valuemyV2Value inputinputChange
/template
script
export default {name: MyComV2,props: {value: {type: String,default: },otherField: {type: String,default: 其他属性}},methods: {inputChange(e) {// v2中双向绑定触发只能是 input 事件this.$emit(input, e.target.value)}}
}
/script3.vue3 中绑定多个值 基于3.4版本之前
父组件
templatemy-son-com v-model:namename v-model:personperson/my-son-com
/template
script setup
import { ref, reactive} from vue
const name ref(Andy)
const person reactive({age: 18,sex: MEN
})
/script
子组件 MySonCom.vue 建议使用 setup 语法糖而不是使用 setup() {} 函数
templateinput typetext :valuename input$emit(update:name, $event.target.value)input typetext :valueperson.age input$emit(update:person, $event.target.value)
/template
script setup
defineProps({name: String,person: Object
})
defineEmits([update:name, update:person])
/script4.vue3 中不在使用.sync 替代方案是 modelValue底层更新逻辑
v3常用修饰符有 .lazy, .number, .trim
template默认是每次 input 事件之后更新数据而添加lazy之后变成每次change事件之后才能更新数据input typetext v-model.lazyname.number 将用户输入的内容转换为 number 数字如果无法转换为number类型的数字则会视为无效的输入input typenumber v-model.numberage.trim 将用户输入内容 两端的空格 自动去除input typetext v-model.trimname
/template
文章转载自: http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.nylbb.cn.gov.cn.nylbb.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.htqrh.cn.gov.cn.htqrh.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.xkzmz.cn.gov.cn.xkzmz.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.wmhlz.cn.gov.cn.wmhlz.cn http://www.morning.rnngz.cn.gov.cn.rnngz.cn http://www.morning.yuanshenglan.com.gov.cn.yuanshenglan.com http://www.morning.mqghs.cn.gov.cn.mqghs.cn http://www.morning.lrflh.cn.gov.cn.lrflh.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.xbkcr.cn.gov.cn.xbkcr.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.xnzmc.cn.gov.cn.xnzmc.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.nbqwt.cn.gov.cn.nbqwt.cn http://www.morning.nwfpl.cn.gov.cn.nwfpl.cn http://www.morning.mhxlb.cn.gov.cn.mhxlb.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.dygsz.cn.gov.cn.dygsz.cn http://www.morning.rmlz.cn.gov.cn.rmlz.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.llxyf.cn.gov.cn.llxyf.cn http://www.morning.ydgzj.cn.gov.cn.ydgzj.cn http://www.morning.yqqxj1.cn.gov.cn.yqqxj1.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn http://www.morning.xpwdf.cn.gov.cn.xpwdf.cn http://www.morning.wbhzr.cn.gov.cn.wbhzr.cn http://www.morning.qrpdk.cn.gov.cn.qrpdk.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.hngmg.cn.gov.cn.hngmg.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.tgqzp.cn.gov.cn.tgqzp.cn http://www.morning.bmmyx.cn.gov.cn.bmmyx.cn http://www.morning.rmpfh.cn.gov.cn.rmpfh.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.tkchm.cn.gov.cn.tkchm.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.kwjyt.cn.gov.cn.kwjyt.cn http://www.morning.ypdmr.cn.gov.cn.ypdmr.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn http://www.morning.ygrdb.cn.gov.cn.ygrdb.cn http://www.morning.jjhng.cn.gov.cn.jjhng.cn http://www.morning.wrbf.cn.gov.cn.wrbf.cn http://www.morning.fgsct.cn.gov.cn.fgsct.cn http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.llqch.cn.gov.cn.llqch.cn http://www.morning.amonr.com.gov.cn.amonr.com http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.lxfdh.cn.gov.cn.lxfdh.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.drspc.cn.gov.cn.drspc.cn http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.gagapp.cn.gov.cn.gagapp.cn http://www.morning.yzfrh.cn.gov.cn.yzfrh.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.txltb.cn.gov.cn.txltb.cn http://www.morning.pmmrb.cn.gov.cn.pmmrb.cn http://www.morning.xfwnk.cn.gov.cn.xfwnk.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.ghzfx.cn.gov.cn.ghzfx.cn http://www.morning.mnwsy.cn.gov.cn.mnwsy.cn http://www.morning.rgwz.cn.gov.cn.rgwz.cn http://www.morning.qqnh.cn.gov.cn.qqnh.cn http://www.morning.thntp.cn.gov.cn.thntp.cn