当前位置:网站首页>atguigu----17-生命周期
atguigu----17-生命周期
2022-06-25 07:37:00 【张 邵】
生命周期引入
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>引出生命周期</title>
<!-- 引入Vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!-- 生命周期: 1.又名:生命周期回调函数、生命周期函数、生命周期钩子。 2.是什么:Vue在关键时刻帮我们调用的一些特殊名称的函数。 3.生命周期函数的名字不可更改,但函数的具体内容是程序员根据需求编写的。 4.生命周期函数中的this指向是vm 或 组件实例对象。 -->
<!-- 准备好一个容器-->
<div id="root">
<h2 v-if="a">你好啊</h2>
<h2 :style="{opacity}">欢迎学习Vue</h2>
</div>
</body>
<script type="text/javascript"> Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。 new Vue({
el:'#root', data:{
a:false, opacity:1 }, methods: {
}, //Vue完成模板的解析并把初始的真实DOM元素放入页面后(挂载完毕)调用mounted mounted(){
console.log('mounted',this) setInterval(() => {
this.opacity -= 0.01 if(this.opacity <= 0) this.opacity = 1 },16) }, }) //通过外部的定时器实现(不推荐) /* setInterval(() => { vm.opacity -= 0.01 if(vm.opacity <= 0) vm.opacity = 1 },16) */ </script>
</html>
生命周期图解

分析生命周期
<!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"> Vue.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>
总结生命周期
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>引出生命周期</title>
<!-- 引入Vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!-- 常用的生命周期钩子: 1.mounted: 发送ajax请求、启动定时器、绑定自定义事件、订阅消息等【初始化操作】。 2.beforeDestroy: 清除定时器、解绑自定义事件、取消订阅消息等【收尾工作】。 关于销毁Vue实例 1.销毁后借助Vue开发者工具看不到任何信息。 2.销毁后自定义事件会失效,但原生DOM事件依然有效。 3.一般不会在beforeDestroy操作数据,因为即便操作数据,也不会再触发更新流程了。 -->
<!-- 准备好一个容器-->
<div id="root">
<h2 :style="{opacity}">欢迎学习Vue</h2>
<button @click="opacity = 1">透明度设置为1</button>
<button @click="stop">点我停止变换</button>
</div>
</body>
<script type="text/javascript"> Vue.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>
边栏推荐
- How to calculate the positive and negative ideal solution and the positive and negative ideal distance in TOPSIS method?
- linux中的mysql有10061错误怎么解决
- Wechat applet opening customer service message function development
- Sampling strategy and decoding strategy based on seq2seq text generation
- 浏览器查看当前页面所有的监听事件
- 在哪个平台买股票开户安全?求分享
- Is it safe to open an account for stocks on the Internet? Can the securities account be used by others?
- Go语言学习教程(十三)
- QSS 不同风格的按钮
- 以科技赋能设计之美,vivo携手知名美院打造“产学研”计划
猜你喜欢

What about the exponential smoothing index?

Fault: 0x800ccc1a error when outlook sends and receives mail

浏览器查看当前页面所有的监听事件

Establish open data set standards and enable AI engineering implementation

Day 5 script and UI System

2021 "Ai China" selection

leetcode. 13 --- Roman numeral to integer

Daily question brushing record (III)

Check whether the point is within the polygon

关于I/O——内存与CPU与磁盘之间的关系
随机推荐
【强化学习笔记】强化学习中的常见符号
Rosparam statement
Jdbc-dao layer implementation
InfluxDB时序数据库
How to calculate the positive and negative ideal solution and the positive and negative ideal distance in TOPSIS method?
物联网毕设(智能灌溉系统 -- Android端)
Is it safe to open an account for stocks on the Internet? Can the securities account be used by others?
What is the file that tp6 automatically executes? What does the tp6 core class library do?
初识生成对抗网络(11)——利用Pytorch搭建WGAN生成手写数字
故障:Outlook 收发邮件时的 0x800CCC1A 错误
Common SRV types
Home server portal easy gate
Go语言学习教程(十三)
Hyper-v:hyper-v generation 1 or generation 2 virtual machines
以科技赋能设计之美,vivo携手知名美院打造“产学研”计划
堆栈认知——栈溢出实例(ret2libc)
在二叉树(搜索树)中找到两个节点的最近公共祖先(剑指offer)
4个不可不知的采用“安全左移”的理由
Measure the current temperature
Prepare these before the interview. The offer is soft. The general will not fight unprepared battles