🔥🔥🔥Vue3中的常用组件通信大总结 包括Vue3.4defineModel()实现组件双向绑定

前言

Vue中官网是这么介绍组件的:组件允许我们将UI划分为独立的、可重用的部分,并且对每个部分进行单独的思考

如果一个文件包含四五个组件,那么他们之间的数据通信如何解决呢,所以组件中数据通信是Vue中非常重要的知识

父传子(子组件使用父组件的 state&& function)

props

xml 复制代码
//父组件
<template>
<div>
这是父组件
</div>
<child :state=state :ParentFn=ParentFn></child>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import child from "./components/index.vue"
const state = ref<string>('这是父组件的state')
const ParentFn =() => {
console.log('这是父组件的方法')
}
</script>
//子组件
<template>
<div>这是子组件</div>
<div>这是父组件传过来的值{{ props.state}}</div>
<button @click="props.ParentFn">触发父组件方法</button>
</template>
<script lang="ts" setup>
const props = defineProps({
	state: String,
	ParentFn:Function,

})
</script>

attrs:包含父作用域里除 class 和 style 除外的非 props 属性集合

xml 复制代码
//父组件
<template>
<child :msg1="msg1" :msg2="msg2" title="good"></child>
</template>
<script setup>
import child from "./components/index.vue"
import { ref, reactive } from "vue"
const msg1 = ref(1)
const msg2 = ref(2)
</script>
//子组件
<template>
<div>{{attrs}}</div>
</template>
<script lang="ts" setup>
import {useAttrs} from "vue"
defineProps({
msg1: Number
})
const attrs = useAttrs()
console.log(attrs) //{ "msg2": 2, "title": "good" }
</script>
//子组件第二种使用方式
//将父组件传递的所有非 prop 属性绑定到子组件的元素上
<template>
<div v-bind="$attrs"></div>
</template>

子传父(父组件使用子组件的state &&function)

emit这个方法我用的比较少 代码比较繁琐且难用

vue 复制代码
//父组件
<template>
<child @myClick="onMyClick"/>
</template>
<script setup lang="ts">
import child from "./components/index.vue"
const onMyClick = (msg:any) => {
console.log(msg) // 这是父组件收到的信息
}
</script>
//子组件
<template>
<div>这是子组件</div>
<button @click="handleClick">按钮</button>
</template>
<script lang="ts" setup>
const emit = defineEmits(["myClick"])
const handleClick = ()=>{
emit("myClick", "这是发送给父组件的信息")
}
</script>

defineExpose + ref

ref绑定在子组件上可以在父组件中来访问子组件的实例

在子组件使用defineExpose暴露方法或变量

xml 复制代码
//父组件
<template>

<div> 这是父组件</div>

<div> 这是子组件的数据{{childRef?.state }}</div>

<button @click="childRef!.fnFromChild">触发子组件</button>

<child ref="childRef"></child>

</template>
<script setup lang="ts">

import { ref } from 'vue';

import child from "./components/index.vue"

const childRef = ref<null | {state:string,fnFromChild:Function}>(null)

</script>

//这是子组件
<template>

<div>这是子组件</div>

</template>
<script lang="ts" setup>
import { ref } from 'vue';
const state = ref<string>("hello")
const fnFromChild = () => {
console.log('这是子组件的方法')
}
defineExpose(
{
state,
fnFromChild
}
)
</script>

v-model: 在子组件直接修改父组件数据

xml 复制代码
//这是父组件

<template>
<div> 这是父组件{{ params }}</div>
<child v-model="params"></child>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import child from "./components/index.vue"
const params = ref<string>('1')
</script>
//这是子组件
<template>
<div>这是子组件{{ modelValue }}</div>
</template>
<script lang="ts" setup>
defineProps({
modelValue: String
})
//实现了父传子

</script>
// 实现子组件修改父组件数据
<template>
<div>这是子组件{{ modelValue }}</div>
<button @click="updateValue">子组件的方法 改变父组件数据</button>
</template>

<script lang="ts" setup>
defineProps({
modelValue: String
})
const emit = defineEmits(["update:modelValue"])
const updateValue = () => {
emit("update:modelValue","我是父组件的值 我被子组件改变的")
}
</script>

v-model在组件上使用双向绑定

从 Vue 3.4 开始,推荐的实现方式是使用 defineModel()其实就是上述例子的语法糖

defineModel() 返回的值是一个 ref, 它的 .value 和父组件的 v-model 的值同步

⚠️ 注意 如果为 defineModel prop 设置了一个 default 值且父组件没有为该 prop 提供任何值,会导致父组件与子组件之间不同步

xml 复制代码
//父组件
<template>
<div> 这是父组件{{ count}}</div>
<child v-model="count"></child>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import child from "./components/index.vue"
const count= ref<number>(1)
</script>

//子组件
<template>
<div>这是子组件{{ model}}</div>
<button @click="update">子组件的方法 改变父组件数据</button>
</template>
<script lang="ts" setup>
const model = defineModel({default : 1})
function update() {
model.value++
}
</script>

组件上的 v-model 也可以接受一个参数

xml 复制代码
//父组件
<child v-model:title="bookTitle" />
//子组件
<script setup>
const title = defineModel('title')
</script>

<template>
  <input type="text" v-model="title" />
</template>

多个v-model绑定

xml 复制代码
//父组件
<child v-model:first-name="first" v-model:last-name="last" />
//子组件
<script setup>
const firstName = defineModel('firstName')
const lastName = defineModel('lastName')
</script>

<template>
  <input type="text" v-model="firstName" />
  <input type="text" v-model="lastName" />
</template>

祖孙通信 provide && inject

用于提供可以被后代组件注入的值

这个组件库用的多些,因为组件库不知道你会在哪使用它提供的值

xml 复制代码
//先代组件
<script setup>
import { ref, provide } from 'vue'
import { countSymbol } from './injectionSymbols'
// 提供静态值
provide('path', 'good')
// 提供响应式的值
const count = ref(0)
provide('count', count)
// 提供时将 Symbol 作为 key
provide(countSymbol, count)
</script>
//后代组件
<script setup>
import { inject } from 'vue'
import { countSymbol } from './injectionSymbols'
// 注入不含默认值的静态值
const path = inject('path')
// 注入响应式的值
const count = inject('count')
// 通过 Symbol 类型的 key 注入
const count2 = inject(countSymbol)
// 注入一个值,若为空则使用提供的默认值
const bar = inject('path', '/default-path')
</script>

全局通信 Pinia

接下来贴一波日常使用的代码 创建文件夹stroe 创建文件index.ts

ts 复制代码
//这样引入方便pinia使用插件比如
// 比如持久化 pinia.use(piniaPluginPersistedstate)
import { createPinia } from 'pinia'
const pinia = createPinia()
export default pinia
//在main.ts中
import pinia from '@/stores'
app.use(pinia)

创建文件user.ts

ts 复制代码
import { defineStore } from 'pinia'
import { UserState } from '@/stores/interface'
export const useUserStore = defineStore('Oner-user', {
state: (): UserState => ({
token: '',
userInfo: { name: 'Oner' },
userVerify: '',
currentPage: 0,
}),
getters: {},
actions: {
// Set Token
setToken(token: string) {
this.token = token
},
// Set setUserInfo
setUserInfo(userInfo: UserState['userInfo']) {
this.userInfo = userInfo
},
setUserVerify(verify: string) {
this.userVerify = verify
},
setCurrentPage(value: number) {
this.currentPage = value
},
},
})

在任意组件中使用

ts 复制代码
const userStore = useUserStore()
console.log(userStore.token) // ''
console.log(userStore.setToken("zhaimoudetoken"))
console.log(userStore.token) // zhaimoudetoken

文章到这里就结束了,希望对你有所帮助。

相关推荐
极客先躯12 分钟前
智能体开发高级面试每日一题|2026-06-04|Agent 的规划(Planning)能力来自哪里?LLM 原生规划有哪些局限?
人工智能·面试·agent·每日一题·高级面试·智能体开发·agenticworkflow
阿策小和尚12 分钟前
把 Python 3.14 + FastAPI + Nuxt 4 压成 164MB 单包迁进 Dopame OS:4 个坑和一份真实体积账单
前端·人工智能·前端框架
极客先躯13 分钟前
智能体开发高级面试每日一题|2026-06-02|为什么说“Agent 的能力上限由 LLM 决定“?工程层面可以做哪些弥补?
人工智能·面试·大模型·llm·agent·高级面试题·智能体开发
map_3d_vis43 分钟前
百度地图画圆 Circle 和矩形 Rectangle(JSAPI 4.0)
前端·javascript·gis·地图·百度地图
AI开源软件1 小时前
SpringBoot多系统同步:别让“同名数据”变成“同一条数据”
vue.js·spring boot·企业管理·mdm
code2cat1 小时前
Vue与React Native任务验收:断网改了标题,重新联网后该看到什么?
vue.js·react native·react.js
FYKJ_20101 小时前
springboot网上汽车租赁系统93523-计算机毕业设计、课程设计
java·vue.js·spring boot·后端·python·汽车·课程设计
lgbisha2 小时前
银河麒麟:软链接还在却打不开,怎么查它指向了哪里
linux·服务器·前端·银河麒麟
@atweiwei10 小时前
LangChainRust 0.33.0 发布:让 Agent 应用更容易运行、观察和评测
服务器·前端·javascript
yuki蜜语10 小时前
「阴升阳降」不符合物理常识?我用一台热虹吸做了个可拆解的 3D 模型
前端·计算机视觉·前端工程化