IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> vue(10) -> 正文阅读

[JavaScript知识库]vue(10)

目录

自定义事件(绑定)

自定义事件(解绑)

组件自定义事件——总结


自定义事件(绑定)

App.vue

<template>
  <div class="app">
    <h1>{{ msg }}</h1>

    <!-- 通过父组件给子组件传递函数类型的props实现,子给父传递数据 -->
    <School :getSchoolName="getSchoolName" />
    
    <!-- v-on在组件student上,所以说给组件的实例对象vc绑定了事件atguigu,如果有人触发了事件,则demo函数会被调用 -->
    <!-- 通过父组件给子组件绑定一个自定义事件实现,子给父传递数据(第一种写法,使用@或v-on) -->
    <!-- <Student v-on:atguigu="getStudentName" /> -->
    <!-- 也能只触发一次 -->
    <Student @atguigu.once="getStudentName" />

    <!-- 通过父组件给子组件绑定一个自定义事件实现,子给父传递数据 (第二种写法,使用ref)-->
    <!-- 优点:更灵活一点,可以用定时器,可以设置只能触发一次 -->
    <!-- <Student ref="student"/> -->

  </div>
</template>

<script>
// 引入school组件
import Student from "./componments/Student.vue";
import School from "./componments/School.vue";

export default {
  name: "App",
  components: { Student, School },
  data() {
    return {
      msg: "你好啊!",
    };
  },
  methods: {
    getSchoolName(name) {
      console.log("App收到了学校名:", name);
    },
    // 除了name,其他东西都用params接收
    getStudentName(name,...params) {
      console.log("App收到了学生名:", name,params);
    },
  },
  mounted(){
    // 定时器
    // setTimeout(()=>{
    // this.$refs.student.$on('atguigu',this.getStudentName)
    // 绑定自定义事件
    // },3000)

    // 绑定自定义事件
    // 只能触发一次
    // this.$refs.student.$once('atguigu',this.getStudentName)
  }
};
</script>
<style scoped>
.app {
  background-color: gray;
  padding: 5px;
}
</style>

Student.vue

<template>
  <div class="student">
    <h2>学生姓名:{{ name }}</h2>
    <h2>学生性别:{{ sex }}</h2>

    <button @click="sendStudentName">把学生名给App</button>
  </div>
</template>

<script>
export default {
  name: "Student",
  data() {
    return {
      name: "张三",
      sex: "男",
    };
  },
  methods:{
    sendStudentName(){
      // 触发Student组件实例对象身上的atguigu事件
      // this.$emit('atguigu',this.name)

      // 如果有很多东西需要传,方法1,直接写
      // this.$emit('atguigu',this.name,666,888,999)

      // 方法2,包装成对象,ES6语法
      this.$emit('atguigu',this.name,666,888,999)
    }
  }
};
</script>
<style lang="less" scoped>
.student {
  background-color: pink;
  padding: 5px;
  margin-top: 30px;
}
</style>

自定义事件(解绑)

?图中意思是当vm被this.destory()时,即被销毁,内部的所有自定义事件,子组件,监视,监听全部被销毁

?Student.vue

<template>
  <div class="student">
    <h2>学生姓名:{{ name }}</h2>
    <h2>学生性别:{{ sex }}</h2>
    <h2>当前求和为:{{number}}</h2>
    <button @click="add">点我number++</button>
    <button @click="sendStudentName">把学生名给App</button>
    <button @click="unbind">解绑atguigu事件</button>
    <button @click="death">销毁当前Student组件的实例(vc)</button>

  </div>
</template>

<script>
export default {
  name: "Student",
  data() {
    return {
      name: "张三",
      sex: "男",
      number:0
    };
  },
  methods:{
    add(){
      console.log('add回调被调用了');
      this.number++
    },
    sendStudentName(){
      // 触发Student组件实例对象身上的atguigu事件
      // this.$emit('atguigu',this.name)

      // 如果有很多东西需要传,方法1,直接写
      // this.$emit('atguigu',this.name,666,888,999)

      // 方法2,包装成对象,ES6语法
      this.$emit('atguigu',this.name,666,888,999)
      this.$emit('demo')

    },
    unbind(){
      // 只能解绑一个事件
      // this.$off('atguigu')

      // 解绑多个自定义事件
      // this.$off(['atguigu','demo'])

      // 解绑所有的自定义事件
      this.$off()
    },
    death(){
      // 销毁了当前Student组件的实例
      // 销毁后所有Student实例的自定义事件全都不奏效
      this.$destroy()
    }
  }
};
</script>
<style lang="less" scoped>
.student {
  background-color: pink;
  padding: 5px;
  margin-top: 30px;
}
</style>

main.js

// 引入Vue
import Vue from 'vue'
// 引入App
import App from './App.vue'

// 关闭Vue的生产提示
Vue.config.productionTip=false

// 创建vm
new Vue({
    el:'#app',
    render:h=>h(App),
    mounted() {
        setTimeout(()=>{
            this.$destroy()
        },3000)
    },
})

以上代码使用了定时器,3秒过后,app被销毁

组件自定义事件——总结

插值语法的来源:

  1. data
  2. props
  3. computed

?1.一种组件间通信的方式,适用于:子组件===>父组件

子组件会触发父组件的事件(回调函数),所以自定义事件是子组件传给父组件。回调函数是留在父组件

2.使用场景:A是父组件,B是子组件,B想给A传数据,那么就要在A中给B绑定自定义事件(事件的回调在A中)

3.绑定自定义事件:

  • 第一种方式,在父组件中:<Demo @athuihu="test"/> 或 <Demo v-on:atguigu="test">
  • 第二种方式,在父组件中:
<Demo ref="demo"/>
......
mounted(){
this.$refs.xxx.$on('atguigu',this.test)
}
  • 若想让自定义事件只能触发一次,可以使用once修饰符,或$once方法

4.触发自定义事件:this.$emit('atguigu',数据)

5.解绑自定义事件:this.$off('atguigu')

6.组件也可以绑定原生DOM事件,需要使用native修饰符

7.注意:通过this.$refs.xxx.$on('atguigu',回调)绑定自定义事件,回调要么配置在methods中,要么箭头函数,否则this指向会出问题

    <!-- 对于组件除了可以设置自定义事件,还可以使用原生dom事件 -->
    <Student ref="student" @click.native="show"/>
    // 第二种方法
    this.$refs.student.$on('atguigu',(name,...params)=>{
      console.log('App收到了学生名:',name,params);
      console.log(this);
      this.studentName=name
    })

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2022-10-08 20:32:10  更:2022-10-08 20:36:17 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 16:43:40-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码