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>
相关文章
|
4天前
|
JavaScript API
如何使用Vue 3和Type Script进行组件化设计
【8月更文挑战第16天】如何使用Vue 3和Type Script进行组件化设计
13 3
|
4天前
|
JavaScript API
如何使用Vue 3和Type Script进行组件化设计
【8月更文挑战第16天】如何使用Vue 3和Type Script进行组件化设计
13 1
|
17天前
|
资源调度 JavaScript 前端开发
Vue3+TypeScript前端项目新纪元:揭秘高效事件总线Mitt,轻松驾驭组件间通信的艺术!
【8月更文挑战第3天】Vue3结合TypeScript强化了类型安全与组件化开发。面对大型应用中复杂的组件通信挑战,可通过引入轻量级事件发射器Mitt实现事件总线模式。Mitt易于集成,通过简单几步即可完成安装与配置:安装Mitt、创建事件总线实例、并在组件中使用`emit`与`on`方法发送及监听事件。此外,利用TypeScript的强大类型系统确保事件处理器正确无误。这种方式有助于保持代码整洁、解耦组件,同时提高应用的可维护性和扩展性。不过,在大规模项目中需谨慎使用,以防事件流过于复杂难以管理。
33 1
|
19天前
|
开发框架 前端开发 JavaScript
在基于vue-next-admin的Vue3+TypeScript前端项目中,为了使用方便全局挂载对象接口
在基于vue-next-admin的Vue3+TypeScript前端项目中,为了使用方便全局挂载对象接口
|
19天前
|
开发框架 前端开发 JavaScript
在Vue3+TypeScript 前端项目中使用事件总线Mitt
在Vue3+TypeScript 前端项目中使用事件总线Mitt
|
11天前
|
JavaScript 测试技术 API
Vue 3 与 TypeScript:最佳实践详解
Vue 3 与 TypeScript:最佳实践详解
|
17天前
|
JavaScript 前端开发 API
Vue 3+TypeScript项目实战:解锁vue-next-admin中的全局挂载对象接口,让跨组件共享变得高效而优雅!
【8月更文挑战第3天】在构建Vue 3与TypeScript及vue-next-admin框架的应用时,为提高多组件间共享数据或方法的效率和可维护性,全局挂载对象接口成为关键。本文通过问答形式介绍其必要性和实现方法:首先定义全局接口及其实现,如日期格式化工具;接着在`main.ts`中通过`app.config.globalProperties`将其挂载;最后在组件内通过Composition API的`getCurrentInstance`访问。这种方式简化了跨组件通信,增强了代码复用性和维护性。
12 0
|
1月前
|
前端开发 JavaScript 安全
TypeScript在React Hooks中的应用:提升React开发的类型安全与可维护性
【7月更文挑战第17天】TypeScript在React Hooks中的应用极大地提升了React应用的类型安全性和可维护性。通过为状态、依赖项和自定义Hooks指定明确的类型,开发者可以编写更加健壮、易于理解和维护的代码。随着React和TypeScript的不断发展,结合两者的优势将成为构建现代Web应用的标准做法。
|
8天前
|
JavaScript
TypeScript——不能将类型“HTMLElement | null”分配给类型“HTMLElement”
TypeScript——不能将类型“HTMLElement | null”分配给类型“HTMLElement”
19 4
|
12天前
|
JavaScript 编译器
typescript 解决变量多类型访问属性报错--工作随记
typescript 解决变量多类型访问属性报错--工作随记