首页 > 其他分享 >027.Vue3入门,父页面接收子页面传过来的事件和参数

027.Vue3入门,父页面接收子页面传过来的事件和参数

时间:2024-08-11 15:09:28浏览次数:5  
标签:vue Child Father 027 Vue3 msg data 页面

1、App.vue代码:

<template>
  <Father/>
</template>

<script setup>
import Father from './view/Father.vue'
</script>

<style>
</style>

2、Father.vue代码:

<template>
  <h3>父页面</h3>
  <Child @myEvent="GetHandle"/>
  <p>父元素接收: {{ msg }}</p>
</template>

<script>
import Child from './Child.vue';

export default {
  data() {
    return {
      msg: ''
    }
  },
  components: {
    Child
  },
  methods: {
    GetHandle(data) {
      console.log('父页面被触发,参数:', data);
      this.msg = data;
    }
  }
}
</script>


<style scoped>

</style>

3、子页面代码:

<template>
  <h3>子页面</h3>
  <button @click="clickEventHandle">子页面按钮</button>
</template>

<script>
export default {
  // 父页面过来的方法,子组件要用emits来声明需要抛出的事件
  emits: ["myEvent"],
  methods: {
    clickEventHandle() {
      //自定义事件
      this.$emit('myEvent', '我是子页面内容');
      console.log('子页面点击了');
    }
  }
}
</script>

4、效果如下:

 

标签:vue,Child,Father,027,Vue3,msg,data,页面
From: https://www.cnblogs.com/tianpan2019/p/18353458

相关文章

  • 025.Vue3入门,父页面给子页面传递数据,校验Props给出默认值
    1、App.vue代码:<template><Father/></template><scriptsetup>importFatherfrom'./view/Father.vue'</script><style></style>2、Father.vue代码<template><h3>父页面</h3><Chil......
  • 026.Vue3入门,父页面给子页面传递数据,在子页面不能修改,只能改自己的data内容
    1、App.vue代码:<template><Father/></template><scriptsetup>importFatherfrom'./view/Father.vue'</script><style></style>2、Father.vue代码:<template><h3>父页面</h3><Chi......
  • DzzOffice 添加页面水印
    文件:1.core\template\default\common\footer_simple.htm2.core\template\default\common\footer.htm这里以显示用户名水印为示例<scripttype="text/javascript">//需要用到的地方调用就好watermark({watermark_txt:'$_G[username]'})funct......
  • 掌握 Nuxt 3 的页面元数据:使用 definePageMeta 进行自定义配置
    title:掌握Nuxt3的页面元数据:使用definePageMeta进行自定义配置date:2024/8/11updated:2024/8/11author:cmdragonexcerpt:摘要:本文详细介绍Nuxt3框架中definePageMeta的使用方法,包括如何为页面组件定义元数据,如布局、过渡效果、路由中间件等。通过具体示例展示......
  • 018.Vue3入门,sytle中加入scoped只在这个文件中生效
    1、全局代码App.vue如下<scriptsetup>importTestpage001from'./view/Testpage001.vue'importTestpage002from'./view/Testpage002.vue'</script><template><divclass="style1">测试1</div><Testp......
  • 016.Vue3入门,表单输入绑定,以及lazy延时回车才显示
    1、代码如下<template><h3>表单输入绑定</h3><form><!--编辑框内容变化时候,下面标签同步显示编辑框内容--><inputtype="text"v-model:="username"><P>{{username}}</P><!--编辑框内容变化时候,按下回车后,标......
  • 013.Vue3入门,在App.vue中加载显示子页面
    1、App.vue代码如下:<scriptsetup>importTestpage001from'./view/Testpage001.vue'</script><template><Testpage001/></template><style></style>2、如图所示 3、Testpage001的代码如下:<template><......
  • 012.Vue3入门,class属性的几种绑定方法
    1、代码如下:<template><h3>class绑定</h3><div:class="{'active':isActive,'text-danger':hasError}">Class样式绑定1</div><div:class="classObject">Class样式绑定2</div><div......
  • uniapp-微信小程序实现分享给好友功能且动态页面
        uniapp全局设置分享朋友及分享到朋友圈功能,在我们没有配置微信的分享时候,微信小程序的时候可用看到,分享链接和这两个都是置灰的,如果我们想要让别人可以分享或者复制链接分享我们的小程序的话,就想要自己开发和配置。接下来分享全局实现的步骤(不需要在每个页面单......
  • js全屏,监听页面是否全屏
    要检测页面是否处于全屏模式,可以使用JavaScript提供的FullscreenAPI。以下是一个简单的示例代码,演示如何检测页面是否处于全屏模式://检测页面是否处于全屏模式functionisPageFullscreen(){return!!(document.fullscreenElement||document.mozFullScreenElemen......