当前位置: 首页> 文旅> 美景 > 网站建设全包哪家便宜_四川手机网站开发_广州seo服务公司_朋友圈广告推广

网站建设全包哪家便宜_四川手机网站开发_广州seo服务公司_朋友圈广告推广

时间:2025/7/13 19:18:43来源:https://blog.csdn.net/m0_73035591/article/details/146054806 浏览次数:0次
网站建设全包哪家便宜_四川手机网站开发_广州seo服务公司_朋友圈广告推广

我们可以通过使用给组件绑定事件,当组件触发该事件时,就能进行值得返回

我们可以使用v-on属性来给子组件绑定自定义事件,此时该事件就会存在vc中,然后通过this.$emit来触发绑定的事件,

这样就能实现不需要app.vue来给子组件传参的复杂形式来实现方法的调用

也可以使用ref来绑定事件,并且ref更为灵活,并且能实现延迟绑定等功能

App.vue

<template>

  <div class="app">

    <h1>{{msg}}</h1>

    <!-- 通过v-on给子组件绑定一个事件 -->

    <!-- <student v-on:atguigu="sendStudentName"></student> -->

     

    <student ref="student"></student>

    <school :getSchoolName="getSchoolName"></school>

   

    <hr>

  </div>

</template>

<script>

import Student from './components/Student.vue'

import School from './components/School.vue'

export default {

  name: 'App',

  components: { Student, School },

  data(){

    return{

       msg:"hello"

    }

  },

  methods:{

    getSchoolName(name){

      console.log('App收到了学校名',name)

    },

    sendStudentName(name){

      console.log('sendStudentName事件被触发了',name)

    }

  },

  mounted(){

   setTimeout(()=>{

    this.$refs.student.$on('atguigu',this.sendStudentName)

   },3000)

  }

}

</script>

<style>

/* 配置全局样式 */

.app {

background-color: gray;

}

</style>

Student.vue

<template>

  <div class="Student">

    <h2  >学生姓名:{{name}}</h2>

    <h2>学生性别:{{sex}}</h2>

    <button @click="sendStudentName">点击获取学生姓名</button>

  </div>

</template>

<script>

export default {

    name:'Student',

   

    data(){

        return{

          name:'李四',

         sex:""

        }

    },

    methods:{

      sendStudentName(){

        //使用$emit来触发Student组件中vcatguigu方法

        this.$emit('atguigu',this.name)

      }

    }

}

</script>

<style scoped>

.Student{

  background-color: orange;

}

</style>

使用$off来接触绑定的事件

关键字:网站建设全包哪家便宜_四川手机网站开发_广州seo服务公司_朋友圈广告推广

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: