首页 > 其他分享 >#yyds干货盘点#vue3 语法糖setup 兄弟组件传值

#yyds干货盘点#vue3 语法糖setup 兄弟组件传值

时间:2023-03-02 22:31:51浏览次数:40  
标签:info yyds vue mitt setup proxy vue3 import app

使用 mitt

// 全局引入
npm install mitt
或者
cnpm install mitt

在main文件中挂载

import { createApp } from 'vue'
import App from './App.vue'
import mitt from 'mitt' // 导入mitt

const app = createApp(App)
app.config.globalProperties.$mitt = new mitt() // mitt在vue3中挂载到全局
app.mount('#app')

组件1 借助imtt 通过emit传值

<script setup>
import { defineComponent,ref,reactive,getCurrentInstance } from 'vue'
// 兄弟组件传值
let { proxy } = getCurrentInstance()
let brother = ref(100)
function sendBrotherData() {
// 通过暴露info 传递 brother 的值
proxy.$mitt.emit('info', brother.value)
}
</script>

组件2


<script setup>
import { defineComponent,ref,reactive,getCurrentInstance } from 'vue'

let { proxy } = getCurrentInstance()
// 拿到info,获取info内部的值
proxy.$mitt.on('info', (res) => {
console.log(res)
// 打印 100
})
</script>

标签:info,yyds,vue,mitt,setup,proxy,vue3,import,app
From: https://blog.51cto.com/u_11365839/6096817

相关文章

  • vue3自动引入插件
    unplugin-auto-import/vite配置完成之后使用refreactivewatch等无须import导入可以直接使用installnpmi-Dunplugin-auto-import vite配置//vite.confi......
  • Vue3 常见错误
    1.UncaughtSyntaxError:Therequestedmodule'/node_modules/.vite/vue-router.js?v=4b09f9b8'doesnotprovideanexportnamed'default'【解决】vue-router的配......
  • #yyds干货盘点 【React工作记录三】React中如何跳转页面传参(参数较短)
     目录​​前言​​​​需求​​​​ 总结​​前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他......
  • #yyds干货盘点 【React工作记录四】React中如何跳转页面传参localStorage和sessionSto
     目录​​前言​​​​需求​​​​ 解决方案​​前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超......
  • vue3注册组件,以及组件之间通信
     注册组件 全局组件 局部组件 递归组件 组件通信 父传子父传递<template><divclass="container"><!--传递数据这里传了一个string和一......
  • 基于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><......
  • 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......