• Vue3表单输入绑定&生命周期


    官网:https://cn.vuejs.org/guide/essentials/forms.html#checkbox

    复选框

    在这个例子中,checkedNames 数组将始终包含所有当前被选中的框的值。

    const checkedNames = ref([])
    
    <div>Checked names: {{ checkedNames }}</div>
    
    <input type="checkbox" id="jack" value="Jack" v-model="checkedNames">
    <label for="jack">Jack</label>
    
    <input type="checkbox" id="john" value="John" v-model="checkedNames">
    <label for="john">John</label>
    
    <input type="checkbox" id="mike" value="Mike" v-model="checkedNames">
    <label for="mike">Mike</label>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    选择器

    <div>Selected: {{ selected }}div>
    
    <select v-model="selected">
      <option disabled value="">Please select oneoption>
      <option>Aoption>
      <option>Boption>
      <option>Coption>
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    注意: 如果 v-model 表达式的初始值不匹配任何一个选择项, 元素触发 input 事件时将值的首字母大写:

    <script setup>
    const props = defineProps({
      modelValue: String,
      modelModifiers: { default: () => ({}) }
    })
    
    const emit = defineEmits(['update:modelValue'])
    
    function emitValue(e) {
      let value = e.target.value
      if (props.modelModifiers.capitalize) {
        value = value.charAt(0).toUpperCase() + value.slice(1)
      }
      emit('update:modelValue', value)
    }
    script>
    
    <template>
      <input type="text" :value="modelValue" @input="emitValue" />
    template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    对于又有参数又有修饰符的 v-model 绑定,生成的 prop 名将是 arg + "Modifiers"。举例来说:

    <MyComponent v-model:title.capitalize="myText">
    
    • 1

    相应的声明应该是:

    const props = defineProps(['title', 'titleModifiers'])
    defineEmits(['update:title'])
    
    console.log(props.titleModifiers) // { capitalize: true }
    
    • 1
    • 2
    • 3
    • 4

    生命周期

    API: https://cn.vuejs.org/api/composition-api-lifecycle.html#onupdated

    <script setup>
    import { onMounted } from 'vue'
    
    onMounted(() => {
      console.log(`the component is now mounted.`)
    })
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

  • 相关阅读:
    【圆满落幕】IDCF社区&天津理工大学华信软件学院校友会技术沙龙丨IDCF
    JAVA8新特性- 函数式接口
    叕跨域了...
    机器学习课后习题 --- 逻辑回归
    微信支付后页面跳转
    AimBetter洞察您的数据库,DPM 和 APM 解决方案
    浪潮云海首席科学家张东:面向一云多芯的系统设计方法
    c++游戏常用函数
    国开大学教育学形考任务
    Web学习笔记-React(路由)
  • 原文地址:https://blog.csdn.net/weixin_43960767/article/details/128135851