【vue】defineEmits 传值 子传父

先行知识

传值流程


App.vue

html 复制代码
<template>
  <Header @getWeb="emitsGetWeb" @userAdd="emitsUserAdd"/>
  <hr />
  <p>web.name: {{ web.name }}</p>
  <p>web.url: {{ web.url }}</p>
  <p>user: {{ user }}</p>
</template>

<script setup>
import { ref, reactive } from "vue";
import Header from "./components/Header.vue";

//响应式数据
const web = reactive({
  name: "1234567890",
  url: "https://www.1234567890.com",
});
let user = ref(0);

const emitsGetWeb = (data) => { 
  console.log(data);
  web.name = data.name;
  web.url = data.url;
}
const emitsUserAdd = (data) => {
  console.log(data);
  user.value += data;
  console.log(user.value);
}
</script>

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

Header.vue

html 复制代码
<template>
    <h2>header</h2>
    <button @click="userAdd">添加用户</button>
</template>

<script setup>

const emits = defineEmits(['getWeb', "userAdd"])

emits("getWeb", { name: "Header Name" , url: "Header Url"})

const userAdd = () => {
    emits("userAdd",1)
}
</script>

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

参考

https://www.bilibili.com/video/BV1nV411Q7RX

相关推荐
九九落12 分钟前
前端获取经纬度完全指南:从Geolocation API到地图集成
前端·获取经纬度
来恩100326 分钟前
jQuery选择器
前端·javascript·jquery
前端繁华如梦28 分钟前
树上挂苹果还是挂玻璃球?Three.js 程序化果实的完整实现指南
前端·javascript
墨痕诉清风35 分钟前
Web浏览器客户端检测网站网络健康(代码)
前端·网络·测试工具
IMPYLH38 分钟前
Linux 的 wc 命令
linux·运维·服务器·前端·bash
happybasic1 小时前
Python库升级标准流程~
linux·前端·python
川冰ICE1 小时前
前端工程化深度实战:从Webpack5到Vite5的构建工具演进与选型决策
前端
CDwenhuohuo1 小时前
优惠券组件直接用 uview plus
前端·javascript·vue.js
用户74090472362751 小时前
我用 curl 排查了一次 OpenAI-compatible API 连接失败:401、403、404 分别怎么定位
前端
kft13141 小时前
XSS深度剖析:从弹窗到持久化窃取Cookie
前端·web安全·xss·安全测试