vue学习记录

一、vue 父子组件的生命周期执行顺序

1.父组件 beforeCreate

2.父组件 created

3.父组件 beforeMount

4.子组件 beforeCreate

5.子组件 created

6.子组件 beforeMount

7.子组件 mounted

8.父组件 mounted

销毁:

1.父组件 beforeDestroy

2.子组件 beforeDestroy

3.子组件 destroyed

4.父组件 destroyed

更新:

1.父组件 beforeUpdate

2.子组件 beforeUpdate

3.子组件 updated

4.父组件 updated

二、 父子组件传值(目前只用过一种)

1. 父传子

父组件

<template>
<view>
<Child :name="name" :title="title" @change="change"></Child>
</view>
</template>
<script>
import Child from "../../components/child.vue";
export default {
		components: {
			Child,
		},
        data() {
            return: {
               name: 'vernon',
               title: '大家好'
            }

        },
        methods: {
			change(e) {
				console.log(e)
				const that = this
				that.title = e
			},

}
</script>

子组件

子组件从props里拿父组件传过来的值

<template>
<view>
<view>我是{{name}},{{title}}。</view>
<button @tap="changeTitle"></button>
</view>
</template>
<script>
	const app = getApp()
	export default {
		name: "child",
		props: {
			name: {
				type: String,
				default:''
			},
            title: {
				type: String,
				default:''
			},
		},
		data() {
			return {
				
			}
		},
        methods: {
            changeTitle() {
                 this.$emit('change', '你们好')
            }
        }
		
	}
</script>

2. 子传父

子组件通过使用$emit发射事件

that.$emit("change", '你们好');

父组件: 拿到子组件的传值

change(e) {
	console.log(e)
	const that = this
	that.title = e
},

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值