Vue3+TypeScript学习笔记(二十二)

简介: Vue3深入v-model

本节主要探讨父子组件使用v-model绑定数据的方式。由于props是单向数据流,因此子组件不可能直接修改来自父组件的props,必须使用自定义事件的方式进行修改。不同的是事件名需要采用Vue3特定的格式update:v-model绑定变量的名字,回调由Vue3事先定义完毕
一个简单的例子:
App.vue

<template>
    <div>
        <div>我是App.vue父组件</div>
        {{ isShow }}
        <button @click="isShow = !isShow">开关</button>
        <br>
        {{ textValue }}
<!-- 这样的写法相当于v-model:value="isShow" -->
        <A v-model="isShow"></A>
        {{ bt }}
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import A from './components/A.vue'

let isShow = ref<boolean>(true)
</script>

<style scoped></style>

A.vue

<template>
    <div v-if="props.modelValue" class="box">
        <div>
            <div class="close"><button @click="close">关闭</button></div>
            <h3>我是v-model子组件</h3>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'

const props = defineProps<{
    // 组件身上的value默认名字是modelValue
    modelValue: boolean,
}>()

// 固定事件名写法,格式为update:我们要暴露出去的名字
const emit = defineEmits(['update:modelValue'])

const close = () => {
    // 通过emit触发事件改变父组件的值,但这个事件名的回调是vue写好的,将第二个参数赋给冒号后面的变量
    emit('update:modelValue',false)
}
</script>

<style scoped>
.box {
    width: 500px;
    height: 200px;
    border: 5px solid #ccc;
}
.close {
    display: flex;
    flex-direction: row-reverse;
}
h3 {
    margin-top: 0;
}
button {
    margin: 10px 10px 0 0;
}
</style>

除了直接使用v-model外,我们也可以自定义绑定的变量名
App.vue

<template>
    <div>
        <div>我是App.vue父组件</div>
        {{ isShow }}
        <button @click="isShow = !isShow">开关</button>
        <br>
        {{ textValue }}
        <A v-model:text="textValue"></A>
        {{ bt }}
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import A from './components/A.vue'

let textValue = ref<string>('小满')
</script>

<style scoped></style>

A.vue

<template>
    <div v-if="props.modelValue" class="box">
        <div>
            <div class="close"><button @click="close">关闭</button></div>
            <h3>我是v-model子组件</h3>
        </div>
        <span>内容:</span><input type="text" @input="change" :value="props.text"/>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'

const props = defineProps<{
    // 自定义参数名
    text:string,
}>()

// 固定事件名写法,格式为update:我们要暴露出去的名字
const emit = defineEmits(['update:text'])

const close = () => {
    // 通过emit触发事件改变父组件的值,但这个事件名的回调是vue写好的,将第二个参数赋给冒号后面的变量
    emit('update:modelValue',false)
}

// 将input的value值回传给父组件
const change = (e:Event) => {
    const target = <HTMLInputElement>e.target
    emit('update:text',target.value)
}
</script>

<style scoped>
.box {
    width: 500px;
    height: 200px;
    border: 5px solid #ccc;
}
.close {
    display: flex;
    flex-direction: row-reverse;
}
h3 {
    margin-top: 0;
}
button {
    margin: 10px 10px 0 0;
}
</style>

此外,Vue3还支持自定义事件修饰符,其表示方法为props绑定的变量名+Modifiers,如果修饰符是可选的还要在后面加上?可选链操作符,具体例子如下:
App.vue

<template>
    <div>
        <div>我是App.vue父组件</div>
        {{ isShow }}
        <button @click="isShow = !isShow">开关</button>
        <br>
        {{ textValue }}
        <A v-model:bt.isBT="bt"></A>
        {{ bt }}
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import A from './components/A.vue'

let bt = ref<string>('默认的bt')
</script>

<style scoped></style>

A.vue

<template>
    <div v-if="props.modelValue" class="box">
        <div>
            <div class="close"><button @click="close">关闭</button></div>
            <h3>我是v-model子组件</h3>
        </div>
        <span>内容:</span><input type="text" @input="change" :value="props.text"/>
        <button @click="call">回传修饰符</button>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'

const props = defineProps<{
    // v-model绑定的bt字符串变量
    bt:string
    // 自定义修饰符,因为是可选的,所以加上?操作符
    btModifiers?:{
        isBT:boolean    // true代表有isBT修饰符,false代表没有
    }
}>()

// 固定事件名写法,格式为update:我们要暴露出去的名字
const emit = defineEmits(['update:bt'])

const close = () => {
    // 通过emit触发事件改变父组件的值,但这个事件名的回调是vue写好的,将第二个参数赋给冒号后面的变量
    emit('update:modelValue',false)
}

// 点击按钮给父组件返回数据。如果存在isBT修饰符就返回小满是BT,如果不存在就返回我是BT
const call = () => {
    // btbtModifiers仅判断修饰符是否存在,事件需要定义在bt身上
    emit('update:bt',props.btModifiers?.isBT ? '小满是BT' : props.bt)
}
</script>

<style scoped>
.box {
    width: 500px;
    height: 200px;
    border: 5px solid #ccc;
}
.close {
    display: flex;
    flex-direction: row-reverse;
}
h3 {
    margin-top: 0;
}
button {
    margin: 10px 10px 0 0;
}
</style>

完整代码:
App.vue

<template>
    <div>
        <div>我是App.vue父组件</div>
        {{ isShow }}
        <button @click="isShow = !isShow">开关</button>
        <br>
        {{ textValue }}
        <A v-model:bt.isBT="bt" v-model:text="textValue" v-model="isShow"></A>
        {{ bt }}
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import A from './components/A.vue'

let isShow = ref<boolean>(true)
let textValue = ref<string>('小满')
let bt = ref<string>('默认的bt')
// 第二十七章已看完,笔记尚未整理
</script>

<style scoped></style>

A.vue

<template>
    <div v-if="props.modelValue" class="box">
        <div>
            <div class="close"><button @click="close">关闭</button></div>
            <h3>我是v-model子组件</h3>
        </div>
        <span>内容:</span><input type="text" @input="change" :value="props.text"/>
        <button @click="call">回传修饰符</button>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'

const props = defineProps<{
    // 组件身上的value默认名字是modelValue
    modelValue: boolean,
    // 自定义参数名
    text:string,
    
    // v-model绑定的bt字符串变量
    bt:string
    // 自定义修饰符,因为是可选的,所以加上?操作符
    btModifiers?:{
        isBT:boolean
    }
}>()

// 固定事件名写法,格式为update:我们要暴露出去的名字
const emit = defineEmits(['update:modelValue','update:text','update:bt'])

const close = () => {
    // 通过emit触发事件改变父组件的值,但这个事件名的回调是vue写好的,将第二个参数赋给冒号后面的变量
    emit('update:modelValue',false)
}

// 将input的value值回传给父组件
const change = (e:Event) => {
    const target = <HTMLInputElement>e.target
    emit('update:text',target.value)
}

// 点击按钮给父组件返回数据。如果存在isBT修饰符就返回小满是BT,如果不存在就返回我是BT
const call = () => {
    // btbtModifiers仅判断修饰符是否存在,事件需要定义在bt身上
    emit('update:bt',props.btModifiers?.isBT ? '小满是BT' : props.bt)
}
</script>

<style scoped>
.box {
    width: 500px;
    height: 200px;
    border: 5px solid #ccc;
}
.close {
    display: flex;
    flex-direction: row-reverse;
}
h3 {
    margin-top: 0;
}
button {
    margin: 10px 10px 0 0;
}
</style>
相关文章
|
13天前
|
JavaScript 前端开发
TypeScript 学习笔记(六):TypeScript 与前端框架的结合应用
笔记,进一步提升 TypeScript 编程技能。
20 1
|
21天前
|
JavaScript 前端开发 编译器
TypeScript 学习笔记
TypeScript 学习笔记
|
21天前
|
JavaScript 前端开发 数据可视化
前端开发使用 Vue 3 + TypeScript + Pinia + Element Plus + ECharts
前端开发使用 Vue 3 + TypeScript + Pinia + Element Plus + ECharts
55 0
|
21天前
|
JavaScript 前端开发
在Vue中使用TypeScript的常见问题有哪些?
在Vue中使用TypeScript的常见问题有哪些?
39 2
|
21天前
|
JavaScript 前端开发
在Vue中使用TypeScript的优缺点是什么?
在Vue中使用TypeScript的优缺点是什么?
22 0
|
21天前
|
JavaScript
在 Vue 中如何使用 TypeScript?
在 Vue 中如何使用 TypeScript?
17 0
|
21天前
|
JavaScript 安全 容器
Vue3 + setup + TypeScript: 构建现代、类型安全的Vue应用的关键技巧总结
当使用 setup 的时候,组件直接引入就可以了,不需要再自己手动注册
|
21天前
|
JavaScript Go 数据库
用Typescript 的方式封装Vue3的表单绑定,支持防抖等功能。
Vue3 的父子组件传值、绑定表单数据、UI库的二次封装、防抖等,想来大家都很熟悉了,本篇介绍一种使用 Typescript 的方式进行统一的封装的方法。
|
21天前
|
前端开发 JavaScript 测试技术
Vue3+Vite+TypeScript常用项目模块详解(下)
现在无论gitee还是github,越来越多的前端开源项目采用Vue3+Vite+TypeScript+Pinia+Elementplus+axios+Sass(css预编译语言等),其中还有各种项目配置比如eslint 校验代码工具配置等等,而我们想要进行前端项目的二次开发,就必须了解会使用这些东西,所以作者写了这篇文章进行简单的介绍。
|
18小时前
|
JavaScript 前端开发 Java
TypeScript 类型兼容性
TypeScript 类型兼容性