【Vue】基础系列(十六)自定义指令-生命周期详解-更新流程

阅读 10

2022-09-27


和阿牛一起冲Vue

🌕写在前面
🍊博客主页勇敢link牛牛 🎉欢迎关注:🔎点赞👍收藏⭐️留言📝
🌟本文由 勇敢link牛牛 原创
📆首发时间:🌹2022年3月31日🌹
🆕最新更新时间:🎄2022年3月31日🎄
✉️愿你熬过万丈孤独,藏下星辰大海!
📠参考书籍:📚​​《Vue2》​​

文章目录

  • ​​和阿牛一起冲Vue​​
  • ​​自定义指令​​
  • ​​通过外部的定时器实现(不推荐)​​
  • ​​写在定时器写在vm里面,但是在外部调用(不推荐)​​
  • ​​vue完成模板解析,并且把真实的DOM页面放在页面后(挂载完毕)调用mounted​​
  • ​​引出生命周期:​​
  • ​​分析生命周期​​

自定义指令

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自定义指令</title>
</head>
<!-- 1、把绑定的数据放大10倍 -->
<!-- 2、 完成与v-bind相似的功能,并获取焦点-->

<body>
<div id="root">
<h2>当前的n是<span v-text="n"></span></h2>
<h2>放大后的10倍是:<span v-big="n"></span></h2>
<button @click="n++">点我n++</button>
<hr />
<input type="text" v-fbind:value="n">
</div>
</body>
<script src='../vue.js'></script>
<script>.config.productionTip = false;
new Vue({
el: '#root',
data: {
n: 1
},
// 指令与元素成功绑定时
// 当指令所在的模板发送重新解析
directives: {
big(element,) {
element.innerText = binding.value * 10;
},
fbind: {
// 函数名不能写错
// 指令与元素成功绑定时
bind(element,) {
element.value = binding.value;
},
// 指令所在元素插入页面时
inserted(element,) {
element.focus();
},
// 指令所在模板重新被解析
update(element,) {
element.focus();
element.value = binding.value;
},
}

}

});</script>

</html>

通过外部的定时器实现(不推荐)

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<div id="root">
<!-- <h2 :style="{opacity:opacity}">勇敢牛牛,不怕困难</h2> -->
<h2 :style="{opacity}">勇敢牛牛,不怕困难</h2>


</div>
</body>
<script src='vue.js'></script>
<script>.config.productionTip = false;
const vm = new Vue({
el: '#root',
data: {
name: 'jack',
opacity: 1
}

});
setInterval(() => {
vm.opacity -= 0.01;
if (vm.opacity <= 0) {
vm.opacity = 1;
}
}, 16)</script>

</html>

写在定时器写在vm里面,但是在外部调用(不推荐)

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<div id="root">
<!-- <h2 :style="{opacity:opacity}">勇敢牛牛,不怕困难</h2> -->
<h2 :style="{opacity}">勇敢牛牛,不怕困难</h2>


</div>
</body>
<script src='vue.js'></script>
<script>.config.productionTip = false;
const vm = new Vue({
el: '#root',
data: {
name: 'jack',
opacity: 1
},
methods: {
chang() {
setInterval(() => {
this.opacity -= 0.01;
if (vm.opacity <= 0) {
vm.opacity = 1;
}
}, 16)
}
},

});
window.onload = () => {
vm.chang()
}</script>

</html>

vue完成模板解析,并且把真实的DOM页面放在页面后(挂载完毕)调用mounted

引出生命周期:

  1.又名:生命周期回调函数、生命周期函数、生命周期钩子。

  2.是什么:Vue在关键时刻帮我们调用的一些特殊名称的函数。

  3.生命周期函数的名字不可更改,但函数的具体内容是程序员根据需求编写的。

  4.生命周期函数中的this指向是vm 或 组件实例对象。

分析生命周期

【Vue】基础系列(十六)自定义指令-生命周期详解-更新流程_vue.js

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>分析生命周期</title>
<!-- 引入Vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!-- 准备好一个容器-->
<div id="root" :x="n">
<h2 v-text="n"></h2>
<h2>当前的n值是:{{n}}</h2>
<button @click="add">点我n+1</button>
<button @click="bye">点我销毁vm</button>
</div>
</body>

<script type="text/javascript">.config.productionTip = false //阻止 vue 在启动时生成生产提示。

new Vue({
el:'#root',
// template:`
// <div>
// <h2>当前的n值是:{{n}}</h2>
// <button @click="add">点我n+1</button>
// </div>
// `,
data:{
n:1
},
methods: {
add(){
console.log('add')
this.n++
},
bye(){
console.log('bye')
this.$destroy()
}
},
watch:{
n(){
console.log('n变了')
}
},
beforeCreate() {
console.log('beforeCreate')
},
created() {
console.log('created')
},
beforeMount() {
console.log('beforeMount')
},
mounted() {
console.log('mounted')
},
beforeUpdate() {
console.log('beforeUpdate')
},
updated() {
console.log('updated')
},
beforeDestroy() {
console.log('beforeDestroy')
},
destroyed() {
console.log('destroyed')
},
})</script>
</html>

常用的生命周期钩子:

  1.mounted: 发送ajax请求、启动定时器、绑定自定义事件、订阅消息等【初始化操作】。
2.beforeDestroy: 清除定时器、解绑自定义事件、取消订阅消息等【收尾工作】。

关于销毁Vue实例

  1.销毁后借助Vue开发者工具看不到任何信息。
2.销毁后自定义事件会失效,但原生DOM事件依然有效。
3.一般不会在beforeDestroy操作数据,因为即便操作数据,也不会再触发更新流程了。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>引出生命周期</title>
<!-- 引入Vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!-- 准备好一个容器-->
<div id="root">
<h2 :style="{opacity}">欢迎学习Vue</h2>
<button @click="opacity = 1">透明度设置为1</button>
<button @click="stop">点我停止变换</button>
</div>
</body>

<script type="text/javascript">.config.productionTip = false //阻止 vue 在启动时生成生产提示。

new Vue({
el:'#root',
data:{
opacity:1
},
methods: {
stop(){
this.$destroy()
}
},
//Vue完成模板的解析并把初始的真实DOM元素放入页面后(挂载完毕)调用mounted
mounted(){
console.log('mounted',this)
this.timer = setInterval(() => {
console.log('setInterval')
this.opacity -= 0.01
if(this.opacity <= 0) this.opacity = 1
},16)
},
beforeDestroy() {
clearInterval(this.timer)
console.log('vm即将驾鹤西游了')
},
})</script>
</html>


精彩评论(0)

0 0 举报