当前位置:网站首页>组件传参与生命周期
组件传参与生命周期
2022-07-29 05:10:00 【冰眸js】
目录
一·组件传参
1.定义与使用
//CounterCom.vue
<template>
<button>1</button>
</template>
App.vue
01 导入
import CounterCom from './components/CounterCom.vue'02 注册
components:{CounterCom}03 使用
<CounterCom></CounterCom>
<counter-com></counter-com>2.父传子
使用props父传给子的数组是只读的(做默认值,读取显示)不能进行修改
App.vue文件中
<CounterCom :num="10">
CounterCom.vue 组件中
1.接收参数并定义默认值
props:{
"num":{type:Number,default:1}
}2.使用参数num
data(){
return {counter:this.num}
}3.子传父
使用的事件 $emit
//CounterCom.vue
<button @click="counter++; $emit('counterchange',counter)">
// App.vue
<CounterCom @counterchange="n=$event">
$emit(事件名,事件值) 发送一次事件,事件名(counterchange)和事件值(counter) 是自定义的
$event 固定写法,事件的值(counterchange 事件的值,也是子组件$emit的第二个参数)
二·生命周期
1.概念
每个组件从创建到销毁都会经历一系列特定的过程,称为生命周期
把过程执行的回调函数称为生命周期钩子函数
2.作用
创建后 发起ajax请求
挂载后操作dom
添加事件监听
销毁前移除间隔调用,事件监听
说明:并不是每个生命周期都必须使用
3.生命周期 8个
1.创建前后
beforeCreate 创建前:
特点:有this,没有data与methods方法
created创建后
特点:有data,没有$el,dom节点
用处:ajax请求,定时器,事件监听
2.挂载前后
beforeMount 挂载前
特点:有$el ,数据没有渲染
mounted挂载后
特点:有dom节点,数据也渲染
用处:操作节点,ajax请求
3.更新前后
beforeUpdate更新前
特点: 会执行多次,数据更新,dom节点没有更新
updated 更新完毕
特点: 会执行多次,数据更新,dom节点也更新
4.销毁前后
beforeDestroy 销毁前
特点:数据可以更新,视图已经不更新
用处:移除事件监听,停止定时器
destroyed 销毁完毕
特点:没有this,切换视图与vue实例的联系
边栏推荐
- 【C语言系列】—深度解剖数据在内存中的存储(一) 暑假开篇
- Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"
- Time complexity and space complexity
- Clickhouse learning (IX) Clickhouse integrating MySQL
- ClickHouse学习(一)ClickHouse?
- 167. 两数之和 II - 输入有序数组
- Basic principle of recursion
- 关于局部变量
- 【C语言系列】—文件操作详解(上)
- Camunda 1. Camunda workflow - Introduction
猜你喜欢

Alibaba cloud architect details nine trends in the game industry

ClickHouse学习(六)语法优化

ClickHouse学习(十)监控运行指标

抢先预约 | 阿里云无影云应用线上发布会预约开启

C language first level pointer

哈夫曼树以及哈夫曼编码在文件压缩上的应用

365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions

Clickhouse learning (IV) SQL operation

shell基本操作(下)

实现简单的数据库查询(不完整)
随机推荐
Clickhouse learning (x) monitoring operation indicators
题解:在一个排序数组中查找元素第一个和最后一个的位置 (个人笔记)
Cmu15-213 malloc lab experiment record
Pointer
End of document
Wechat applet video upload component is directly uploaded to Alibaba cloud OSS
ClickHouse学习(十)监控运行指标
【剑指offer】— 详解库函数atoi以及模拟实现atoi函数
365 day challenge leetcode 1000 questions - day 040 design jump table + avoid flooding + find the latest grouping with size M + color ball with reduced sales value
GPIO的输入输出详解
牛客网编程题—【WY22 Fibonacci数列】和【替换空格】详解
Clickhouse learning (V) cluster operation
【C语言系列】—文件操作详解(上)
Bubble sort c language
Alibaba cloud and Dingjie software released the cloud digital factory solution to realize the localized deployment of cloud MES system
ClickHouse学习(三)表引擎
365 day challenge leetcode1000 question - distance between bus stops on day 038 + time-based key value storage + array closest to the target value after transforming the array and + maximum value at t
个人学习笔记
Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction
js深拷贝-笔记