1. 组件的生命周期
生命周期(Life Cycle)是指一个组件从创建 -> 运行 -> 销毁的整个阶段,强调的是一个时间段。
生命周期函数:是由 vue 框架提供的内置函数,会伴随着组件的生命周期,自动按次序执行。

注意:生命周期强调的是时间段,生命周期函数强调的是时间点。
2. 组件之间的数据共享
2.1 组件之间的关系
在项目开发中,组件之间的最常见的关系分为如下两种:
① 父子关系
② 兄弟关系

2.2 父子组件之间的数据共享
父子组件之间的数据共享又分为:
① 父 -> 子共享数据
② 子 -> 父共享数据
2.3 父组件向子组件共享数据
父组件向子组件共享数据需要使用自定义属性。示例代码如下:

父组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <template> <div id="app"> <TestVue :msg="message" :user="userinfo"></TestVue> </div> </template>
<script> import TestVue from './components/TestVue.vue'
export default { name: 'App', components: { TestVue }, data() { return { message: 'hello vue.js', userinfo: { name: 'zs', age: 20 } } } } </script>s
|
子组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <template> <div> <h5>Son 组件</h5> <p>父组件传递过来的 msg 值是:{{ msg }}</p> <p>父组件传递过来的 user 值是:{{ user }}</p> </div> </template>
<script> export default { name: 'TestVue', props: { msg: { type: String, default: '', required: true }, user: { type: Object, default: function() { return {} }, required: true } } } </script>
|
2.4 子组件向父组件共享数据
子组件向父组件共享数据使用自定义事件。示例代码如下:

子组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <template> <div> <button @click="add">加一</button> </div> </template>
<script> export default { name: 'TestEmit', props: { }, data() { return { count: 0 } }, methods: { add() { this.count += 1 this.$emit("numchange", this.count) } } } </script>
|
父组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <template> <div id="app"> <TestEmit @numchange="getNewCount"></TestEmit> <p>countFromSon值为:{{ countFromSon }}</p> </div> </template>
<script> import TestEmit from './components/TestEmit.vue'
export default { name: 'App', components: { TestEmit }, data() { return { countFromSon: 0 } }, methods: { getNewCount(val) { this.countFromSon = val } } } </script>
|
2.5 兄弟组件之间的数据共享
在 vue2.x 中,兄弟组件之间数据共享的方案是 EventBus

EventBus 的使用步骤
① 创建 eventBus.js 模块,并向外共享一个 Vue 的实例对象
② 在数据发送方,调用 bus.$emit('事件名称', 要发送的数据)
方法触发自定义事件
③ 在数据接收方,调用 bus.$on('事件名称', 事件处理函数)
方法注册一个自定义事件
3. ref引用
ref 用来辅助开发者在不依赖于 jQuery 的情况下,获取 DOM 元素或组件的引用。
每个 vue 的组件实例上,都包含一个 $refs 对象,里面存储着对应的 DOM 元素或组件的引用。默认情况下,组件的 $refs 指向一个空对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <template> <div> <h3>MyRef 组件</h3> <button @click="getRef">测试 Refs 引用</button> </div> </template>
<script> export default { name: 'TestRef', props: { }, data() { return { } }, methods: { getRef() { console.log(this) } } } </script>
|
3.1 使用ref引用DOM元素
如果想要使用 ref 引用页面上的 DOM 元素,则可以按照如下的方式进行操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <template> <div> <h3 ref="myh3">MyRef 组件</h3> <button @click="getRef">测试 Refs 引用</button> </div> </template>
<script> export default { name: 'TestRef', props: { }, data() { return { } }, methods: { getRef() { console.log(this) this.$refs.myh3.style.color = 'red' } } } </script>
|
3.2 使用ref引用组件实例
如果想要使用 ref 引用页面上的组件实例,则可以按照如下的方式进行操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <template> <div id="app"> <TestEmit ref="testEmitRef" @numchange="getNewCount"></TestEmit> <button @click="getRef">加一from父组件</button> <p>countFromSon值为: {{ countFromSon }}</p> </div> </template>
<script> import TestEmit from './components/TestEmit.vue'
export default { name: 'App', components: { TestEmit, }, data() { return { countFromSon: 0 } }, methods: { getNewCount(val) { this.countFromSon = val }, getRef() { console.log(this.$refs.testEmitRef) this.$refs.testEmitRef.add() } } } </script>
|
3.3 控制文本框和按钮的按需切换
通过布尔值 inputVisible
来控制组件中的文本框与按钮的按需切换。示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <template> <div> <input type="text" v-if="inputVisible"> <button @click="showInput">展示input输入框</button> <button @click="disableInput">关闭input输入框</button> </div> </template>
<script> export default { name: 'TestInputVisible', props: { }, data() { return { inputVisible: false } }, methods: { showInput() { this.inputVisible = true }, disableInput() { this.inputVisible = false } } } </script>
|
3.4 让文本框自动获得焦点
当文本框展示出来之后,如果希望它立即获得焦点,则可以为其添加 ref 引用,并调用原生 DOM 对象的.focus() 方法即可。示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <template> <div> <input type="text" v-if="inputVisible" ref="ipt"> <button @click="showInput">展示input输入框</button> <button @click="disableInput">关闭input输入框</button> </div> </template>
<script> export default { name: 'TestInputVisible', props: { }, data() { return { inputVisible: false } }, methods: { showInput() { this.inputVisible = true this.$nextTick(() => { this.$refs.ipt.focus() }) }, disableInput() { this.inputVisible = false } } } </script>
|
3.5 this.$nextTick(cb)方法
组件的 $nextTick(cb) 方法,会把 cb 回调推迟到下一个 DOM 更新周期之后执行。通俗的理解是:等组件的DOM 更新完成之后,再执行 cb 回调函数。从而能保证 cb 回调函数可以操作到最新的 DOM 元素。