首页 > 其他分享 >vue3注册组件,以及组件之间通信

vue3注册组件,以及组件之间通信

时间:2023-03-02 15:23:53浏览次数:46  
标签:const list 传递 reactive 注册 vue3 组件 import

 

注册组件

 

全局组件

 

局部组件

 

递归组件

 

组件通信

 

父传子

父传递

<template>
  <div class="container">
     <!-- 传递数据 这里传了一个string 和 一个list -->
    <Hello title="我是hello的爸爸" :list='list'/>
    <hr>
    <h4>子组件传数据过来了 {{fromSon}}</h4>
  </div>
</template>

<script setup>
import { reactive, toRefs } from 'vue'
//导入子组件
import Hello from '@/components/HelloWorld'
const list = reactive([
  { id: 1, name: '哈哈哈' },
  { id: 2, name: '嘿嘿嘿' },
  { id: 3, name: '呵呵呵' },
])
</script>

子接收

<template>
  <div class="container">
    我是Hello
    <h5>父组件传了一句话过来 String---- {{title}}</h5>
    <h5>父组件传了一个数组过来 Array --- {{list}}</h5>
  </div>
</template>
<script setup>
import { reactive, toRefs } from 'vue'
    // 通过defineProps接收,之后直接声明
    defineProps({
      title:String,
      list:Array
    })
</script>

 

子传父:emits传递和ref传递

emits传递

emits传递 子传递

<template>
  <div class="container">
    <button @click="clickTap">点击这里给父组件传递些许数据</button>
  </div>
</template>
<script setup>
import { reactive, toRefs } from 'vue'

// 这里可以传递多个值
  const list = reactive([1,2,3,4])
  // defineEmits内跟一个数组,数据内写绑定的事件
  const emit = defineEmits(['on-click'])
  //进行数据传递
  const clickTap = () => {
    emit('on-click',list,true)
  }
</script>

 

emits传递 父接收

<template>
  <div class="container">
    <!-- 传递数据 这里传了一个string 和 一个list -->
    <Hello @on-click="getList" />
    <hr>
    <h4>子组件传数据过来了 {{fromSon}}</h4>
  </div>
</template>

<script setup>
import { reactive, toRefs } from 'vue'
import Hello from '@/components/HelloWorld'

const fromSon = reactive([])
const getList = (list,flag) => {
  // 这里不能直接复制,会破坏双休绑定数据
  fromSon.push(...list) 
  console.log(flag);
  console.log('子组件传过来的值',list);
}
</script>

<style lang="scss" scoped>
    
</style>

 

通过ref传递

ref传递 子传递

<template>
  <div class="container">
    <button @click="clickTap">点击这里给父组件传递些许数据</button>
  </div>
</template>

<script setup>
import { reactive, toRefs } from 'vue'
// 这里可以传递多个值
const list = reactive([1,2,3,4])
// defineEmits内跟一个数组,数据内写绑定的事件
const emit = defineEmits(['on-click'])
const clickTap = () => {
  emit('on-click')
}
// 暴露出list
defineExpose({
  list
})
</script>

 

ref传递 父接收

<template>
  <div class="container">
    <!-- 传递数据 这里传了一个string 和 一个list -->
    <Hello ref="menus" @on-click="getList" />
  </div>
</template>

<script setup>
import { reactive, toRefs,ref } from 'vue'
import Hello from '@/components/HelloWorld'

// 还可以通过ref进行字传父
const menus = ref(null)    
    
const getList = (list,flag) => {
  console.log(menus.value);
}
</script>

 

兄弟之间

标签:const,list,传递,reactive,注册,vue3,组件,import
From: https://www.cnblogs.com/miangao/p/17171865.html

相关文章

  • 如何注册appuploader账号​
    如何注册appuploader账号​我们上一篇讲到appuploader的下载安装,要想使用此软件呢,需要注册账号才能使用,今​天我们来讲下如何注册appuploader账号来使用软件。​1.Apple......
  • 基于vue3+el-upload 获取视频第一帧截图并上传服务器
    //视频上传成功consthandleVideoSuccess:UploadProps['onSuccess']=(response,uploadFile)=>{  if(response.status=='500005'){   detailInfo.v......
  • 学习vue3遇到的问题
    1.toReftoRef是用来给抽离响应式对象(被reactive包裹的对象)中的某一个属性的,并且把这个属性包裹成ref对象,使其和原对象产生链接。或许有人就回有人有疑问了?这个toRef存在......
  • vue3 门户网站搭建6-wangeditor
    门户网站的新闻、公告等文章,内容可配置,故引入wagneditor 1、安装:npmi wangeditor 2、方便调用,抽成组件:<template><divref='editor'></div></template><......
  • video和表单组件(不常用 类型太少)
    h5不能自动播放,只有在静音的前提下才能button:(种类太少不能满足需求)<buttonsize="mini"type="primary"plainloading>确认提交</button>input:......
  • 媒体img组件以及swiper轮播
    .swiper{    height:400rpx;    margin-top:100rpx;    .item{      padding:20rpx;      width:100%;   ......
  • 注册页面及注册测试
    自己慢慢敲得注册页面及注册测试目前没有实现与数据库的联系<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/r......
  • vue3的ref、reactive、toRefs特性详解
    了解ref()、reactive()这两个特性之前,我们先回顾一下vue2中data和method方法。在vue2中我们定义一个响应式变量name,通过点击事件handle来改变name的值是通过如下方式写的。......
  • Vue3 reactive的理解
    1.什么是reactive?reactive是Vue3中提供实现响应式数据的方法.在Vue2中响应式数据是通过defineProperty来实现的.而在Vue3响应式数据是通过ES6的Proxy来实现的2.rea......
  • #创作者激励#【FFH】自制ArkUI组件-文件管理器(二)悬浮小球!
    【本文正在参加2023年第一期优质创作者激励计划】前言交互设计UI整体设计UI自适应布局悬浮窗改造效果图GIFEND前言经过重重的改造封装,这一版的FilerBall组件基本......