【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

相关推荐
摘星编程29 分钟前
React Native + OpenHarmony:Stepper步进器组件
javascript·react native·react.js
●VON32 分钟前
React Native for OpenHarmony:简易计算器应用的开发与跨平台适配实践
javascript·react native·react.js
摘星编程8 小时前
OpenHarmony + RN:Placeholder文本占位
javascript·react native·react.js
a1117768 小时前
医院挂号预约系统(开源 Fastapi+vue2)
前端·vue.js·python·html5·fastapi
0思必得08 小时前
[Web自动化] Selenium处理iframe和frame
前端·爬虫·python·selenium·自动化·web自动化
计算机毕设VX:Fegn08959 小时前
计算机毕业设计|基于springboot + vue蛋糕店管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
行走的陀螺仪10 小时前
uni-app + Vue3编辑页/新增页面给列表页传参
前端·vue.js·uni-app
摘星编程11 小时前
React Native + OpenHarmony:Spinner旋转加载器
javascript·react native·react.js
We་ct11 小时前
LeetCode 205. 同构字符串:解题思路+代码优化全解析
前端·算法·leetcode·typescript
2301_8127314112 小时前
CSS3笔记
前端·笔记·css3