首页 > 其他分享 >vue3组件之间传值

vue3组件之间传值

时间:2023-03-24 10:14:23浏览次数:49  
标签:vue const carr vue3 组件 children 传值

父组件向子组件传值

1.简单的props方式

//fater.vue
<div class="father">
    <children :carr="arr" />
</div> 
  
<script setup lang="ts">
import children from './children.vue'
const arr = [1, 3, 5]
//children.vue
 <div class="children">
    <div>carr:{{ carr }}</div>
  </div>

<script setup lang="ts">
defineProps({
  carr: {
    type: Array,
    required: true
  }
})

子组件向父组件传值

1.函数方式

//fater.vue
<div class="father">
    <children :carr="fatherFun" />
 </div> 

<script setup lang="ts"> 
import children from './children.vue'
const fatherFun = (n: number) => {
  console.log('父组件函数,接收到子组件的值为:', n)
}
//children.vue
 <div class="children">
    <div @click="onUp(3)">carr:</div>
  </div>

<script setup lang="ts"> 
const props = defineProps({
  carr: {
    type: Function,
    required: true
  }
})

function onUp(n: number) {
  console.log('子组件函数')
  props.carr(n)
}
/*
结果为:
子组件函数
父组件函数,接收到子组件的值为: 3
*/

2.emit方式

//father.vue
<div class="father">
    <children :carr="arr" @getChildren="oncFun" />
</div>

<script setup lang="ts">
import children from './children.vue'
const arr = [1, 3, 5]

function oncFun(x: number) {
  console.log('父组件函数,接收到子组件的值为:', x)
}
//children.vue
<div class="children">
    <div @click="onUp(3)">carr:</div>
</div>

<script setup lang="ts">
const props = defineProps({
  carr: {
    type: Array,
    required: true
  }
})

//setup中使用emits,需要先声明,定义属性数量
const emits = defineEmits(['getChildren'])
function onUp(n: number) {
  console.log('子组件函数')
  emits('getChildren', n)
}

3.provide和inject

适用于层级比较多,如爷孙组件传值。结合传函数的方式,可以让爷孙组件相互传值

父组件
provide('stateFun', (x) => {
  console.log('x',x)
})
孙组件
Function代表注入的是函数,true代表有传入参数
const stateFun = inject('stateFun', Function, false)
stateFun('abc')

用provide和inject 会遇上类型问题,子组件接收到的是unknown类型
所以用 InjectionKey 类型声明来定义值(做名称)

**新建一个type.ts**
import { InjectionKey, Ref } from 'vue'
export interface User {
  name: string
  age: number
}
// 对象的InjectionKey
export const userKey: InjectionKey<Ref<User>> = Symbol('')

**父组件中**
const user = ref({ name: 'tom', age: 20 })
provide('user', user)  //普通的传值
provide(userKey, user)  //类型声明的传值

**子组件中**
import { userKey } from './type'
const user: any = inject('user') //user: unknown 无法点操作,所以声明为any
console.log('inject:', user.value.name) //tom
const user2 = inject(userKey) //user2: Ref<User> | undefined
console.log('injectKey:', user2) // RefImpl {...}

vuex或pinia方式



标签:vue,const,carr,vue3,组件,children,传值
From: https://www.cnblogs.com/pansidong/p/17250434.html

相关文章

  • Vue3中使用pinia
    Vue3中使用piniaPinia是一个轻量级的、基于Vue3的状态管理库,它的设计目标是提供简单易用的API,使得开发者能够更加便捷地管理Vue3应用程序中的状态。与Vuex相比,Pinia更加......
  • Spring Cloud Alibaba系列(三)微服务配置管理和服务管理组件Nacos高可用集群的搭建
    网络上Nacos的文章很多,大部分都只说到了怎么搭建单机版本,这里来说说Nacos的集群。Nacos是SpringCLoudALibaba重要组件,起了注册中心和配置中心作用。首先微服务中通过以......
  • react函数组件中,父组件调用子组件的方法
    使用ref来处理。父组件里面   子组件里面   ......
  • Vue3
    Vue3.0Devtools6.5.0开发者工具下载和安装1、官网地址下载:GitHub-vuejs/devtools:⚙️BrowserdevtoolsextensionfordebuggingVue.jsapplications.2、下载安装......
  • 1万条数据只能传值调用存储过程,sql如何优化
    1如果需要传递的数据量比较大,那么在存储过程中使用表变量是一种比较好的选择,而且可以通过以下方式优化:使用临时表代替表变量如果从外部获取的数据量较大,可以考虑使用......
  • vue3+ts的toRef和toRefs用法
    <template><div><h2>toRefs的使用</h2><h3>姓名:{{obj.name}}</h3><h3>年龄:{{obj.age}}</h3><hr/><h3>姓名:{{name}}</h3><h3>......
  • React的组件通信与状态管理
    目录1.组件通讯-概念1.组件的特点2.知道组件通讯意义总结:2.组件通讯-props基本使用1.传递数据和接收数据的过程2.函数组件使用props 3.类组件使用props总......
  • element-ui table 组件的表头加tips 提示信息的方法
    需求:表格有一列是显示比重,用户想知道这个比重数据的计算公式,需要再表头加一个小图标,鼠标放在图标上,显示计算公式。分析:该需求的实现方式有两种,我们说一个比较简单的,且没有......
  • react的组件创建类型
    一、类组件(有状态组件)有props.控制状态state,可以试用生命周期函数1. 类名称必须以大写字母开头 2. 类组件必须继承React.Component父类,从而可以使用父类中提供的......
  • vue3中如何通过遍历传入组件名称动态创建多个component 组件
    背景在vue3中,如果使用component,可以动态加载一个组件,例如<!--直接创建--><component:is="Image"/>这样会将已经定义好并导入的比如Image组件加载出来,但......