vue3.0-setup中的组件通信

① 父传子

--父组件中的代码

<template>
<h1>这是父组件</h1>
  <component :is="sonView" :title="title" :name="name"></component>
</template>
<script setup>
import sonView from '@/components/sonView'
import {ref} from "vue";
const title = ref('这是父组件的标题')
const name = ref('小明')
</script>

--子组件中的代码

<template>
<h1>这是子组件</h1>
  <h2>这是接收的父组件的数据:{{props}}</h2>
  <h3>{{title}}</h3>
  <h3>{{name}}</h3>
</template>
<script setup>
import {toRefs} from "vue";

const props = defineProps(['title','name'])
const {title,name} = toRefs(props)

</script>

②子传父

--子组件中的代码

<template>
<h1>这是子组件</h1>
  <h2>这是接收的父组件的数据:{{props}}</h2>
  <button @click="update">子组件修改父组件的数据</button>
  <h3>{{title}}</h3>
  <h3>{{name}}</h3>
</template>
<script setup>
import {toRefs} from "vue";

const props = defineProps(['title','name'])
const {title,name} = toRefs(props)

let emits = defineEmits(['change'])
const update = ()=>{
  emits("change")
}
</script>

--父组件中的代码

<template>
<h1>这是父组件</h1>
  <h2>Number的数字为:{{num}}</h2>
  <button @click="addNum">增加数字</button>
  <hr>
  <component :is="sonView" :title="title" :name="name"  @change="addNum"></component>
</template>
<script setup>
import sonView from '@/components/sonView'
import {ref,reactive} from "vue";
const title = ref('这是父组件的标题')
const name = ref('小明')

const num = ref(2)
const addNum = ()=>{
  num.value++;
}
</script>

③跨组件传参

--父级组件中的代码

<template>
<h1>这是父组件</h1>
  <h2>Number的数字为:{{num}}</h2>
  <button @click="addNum">增加数字</button>
  <hr>
  <component :is="sonView" :title="title" :name="name"  :info="info" @change="addNum"></component>
  <hr>
  <component :is="grandsonView"  @change="addNum"></component>
</template>
<script setup>
import sonView from '@/components/sonView'
import grandsonView from '@/components/grandsonView'
import {ref,provide,reactive} from "vue";
const title = ref('这是父组件的标题')
const name = ref('小明')
/*provide语法,第一个参数是:key,第二个参数是:value*/
provide('name',name)
provide('info',{
  age:18,
  height:180,
  weight:122
})
const num = ref(2)
const addNum = ()=>{
  num.value++;
}
let info = reactive({
  age:18,
  height:180,
  weight:122
})
</script>

--儿子组件中的代码

<template>
<h1>这是子组件</h1>
  <h2>这是接收的父组件的数据:{{props}}</h2>
  <button @click="update">子组件修改父组件的数据</button>
  <h3>{{title}}</h3>
  <h3>{{name}}</h3>
  <h3>{{props}}</h3>
  年龄:<span>{{info.age}}</span>
  身高:<span>{{info.height}}</span>
  体重:<span>{{info.weight}}</span>
</template>
<script setup>
import {toRefs} from "vue";

const props = defineProps(['title','name','info'])
/*使用toRefs来进行数据的结构,不会破坏响应式数据*/
const {title,name,info} = toRefs(props)
/*使用defineEmits来进行接收传递过来的函数*/
let emits = defineEmits(['change'])
const update = ()=>{
  emits("change")
}
</script>

--孙子组件中的代码

<template>
<h1>这是孙子组件</h1>
  <button @click="update">孙子组件修改父级组件的数据</button>
  <h2>这是父级组件传递过来的参数:{{data}}</h2>
  <h2>这是父级组件传递过来的参数:{{info}}</h2>
  <h2>这是父级组件传递过来的参数:年龄:{{age}},身高:{{height}},体重:{{weight}}</h2>
</template>
<script setup>
import {inject,toRefs} from "vue";
const data = inject('name')
const info = inject('info')
const {age,height,weight} = toRefs(info)
const emits = defineEmits(['change'])
const update = ()=>{
  emits("change")
}
</script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值