咸鱼呀 咸鱼呀
首页
  • 博客文章

    • node搭建简易cli
    • 一个好看的loading
    • CSS-笔记
    • git提交规范
    • JS工具函数-转载
    • Vue组件打包到发布流程
    • nuxt.js-相关配置信息
    • nvm-upgrade
    • NVM 安装 之 window
    • 正则常用表达式
    • vue 3.0 生命周期函数记录
    • vue 对IE的支持
    • vue入门技术分享
    • 解决ios端inout失焦时,底部白底
    • WebStorm配置本地测试服务器
  • 分类
  • 标签
  • 归档
关于
博客 (opens new window)
GitHub (opens new window)

s-xianyu

前端小白
首页
  • 博客文章

    • node搭建简易cli
    • 一个好看的loading
    • CSS-笔记
    • git提交规范
    • JS工具函数-转载
    • Vue组件打包到发布流程
    • nuxt.js-相关配置信息
    • nvm-upgrade
    • NVM 安装 之 window
    • 正则常用表达式
    • vue 3.0 生命周期函数记录
    • vue 对IE的支持
    • vue入门技术分享
    • 解决ios端inout失焦时,底部白底
    • WebStorm配置本地测试服务器
  • 分类
  • 标签
  • 归档
关于
博客 (opens new window)
GitHub (opens new window)
  • 学习笔记
s-xianyu
2021-06-27

vue 3.0 生命周期函数记录

记录3.0常用方法及属性

简单两个页面, 主要是查看生命周期

<template>
	<div class="look">
		<h2>Vue3.0 生命周期函数{{ num }}</h2>
		<h2>见控制台</h2>
		<br>
		<div style="display: flex;">
			<el-button @click="toggleColor">toggleColor</el-button>
			<span style="width:20px"></span>
			<el-button @click="toLook">toggleLook</el-button>
		</div>
	</div>
</template>

<script lang="ts">
import {
	defineComponent,
	onBeforeMount,
	onMounted,
	onBeforeUpdate,
	onUpdated,
	onBeforeUnmount,
	onUnmounted,
	onActivated,
	onDeactivated,
	onErrorCaptured,
	onRenderTracked,
	onRenderTriggered,
	getCurrentInstance,
	reactive,
	toRefs,
    computed,
	ref,
	nextTick
} from 'vue';
import { useRouter, useRoute } from 'vue-router';
export default defineComponent({
	setup () {
		// 全局对象实例
		// ctx.toggleColor(3.0) -- this.toggleColor(2.0)
		const el = getCurrentInstance() as any;
		// Router方法 常用addRoute,beforeEach,afterEach
		const Router = useRouter();
		// route对象,常用params, query取值
		const Route = useRoute();
		// 存储dom数组 需push
		const myRef = ref([]);
		// 定义值
		const num = ref<number>(12);
		// 定义多个值 2.0里相当于data
		const state = reactive({
			colors: 'red' // style里可使用
		});
        // 创建一个计算属性
		const addAge = computed(() => el.ctx.num++ );
		// 生命周期
		onBeforeMount(() => {
			console.log(el);
			console.log(Route.query.id);
			console.log('onBeforeMount', 1);
		});
		onMounted(() => {
			console.log('onMounted', 2);
		});
		onBeforeUpdate(() => {
			console.log('onBeforeUpdate', 3);
		});
		onUpdated(() => {
			console.log('onUpdated', 4);
		});
		onBeforeUnmount(() => {
			console.log('onBeforeUnmount', 5);
		});
		onUnmounted(() => {
			console.log('onUnmounted', 6);
		});
		onActivated(() => {
			console.log('onActivated', 7);
		});
		onDeactivated(() => {
			console.log('onDeactivated', 8);
		});
		onErrorCaptured(() => {
			console.log('onErrorCaptured', 9);
		});
		onRenderTracked((e) => {
			debugger;
			console.log('onRenderTracked', e);
		});
		onRenderTriggered((e) => {
			debugger;
			console.log('onRenderTriggered', e);
		});
		const toLook = () => {
			Router.push({
				name: 'toggleLook'
			});
		};
		const methods = {
			toggleColor: () => {
				state.colors = state.colors === 'red' ? 'blue' : 'red';
			}
		};
		return {
			num,
			toLook,
			...toRefs(state),
			...methods
		};
	}
});
</script>

<style lang="less" scoped>
.look{
	width:100vw;
	height:100vh;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	color: v-bind(colors);
	h2{
		display: flex;
	}
}
</style>

<template>
	<div class="look">
		<h2>Vue3.0 生命周期函数</h2>
		<h2>见控制台</h2>
		<br>
		<el-button @click="toLook">toggleLook</el-button>
	</div>
</template>

<script lang="ts">
import {
	defineComponent,
	onBeforeMount,
	onMounted,
	onBeforeUpdate,
	onUpdated,
	onBeforeUnmount,
	onUnmounted,
	onActivated,
	onDeactivated,
	onErrorCaptured,
	onRenderTracked,
	onRenderTriggered
} from 'vue';
import { useRouter } from 'vue-router';

export default defineComponent({
	setup () {
		const Router = useRouter();
		onBeforeMount(() => {
			console.log('onBeforeMount', 1);
		});
		onMounted(() => {
			console.log('onMounted', 2);
		});
		onBeforeUpdate(() => {
			console.log('onBeforeUpdate', 3);
		});
		onUpdated(() => {
			console.log('onUpdated', 4);
		});
		onBeforeUnmount(() => {
			console.log('onBeforeUnmount', 5);
		});
		onUnmounted(() => {
			console.log('onUnmounted', 6);
		});
		onActivated(() => {
			console.log('onActivated', 7);
		});
		onDeactivated(() => {
			console.log('onDeactivated', 8);
		});
		onErrorCaptured(() => {
			console.log('onErrorCaptured', 9);
		});
		onRenderTracked((e) => {
			debugger;
			console.log('onRenderTracked', e);
		});
		onRenderTriggered((e) => {
			debugger;
			console.log('onRenderTriggered', e);
		});
		const toLook = () => {
			Router.push({
				name: 'look',
				query: {
					id: 'xianyu'
				}
			});
		};
		return {
			toLook
		};
	}
});
</script>

<style lang="less" scoped>
.look{
	width:100vw;
	height:100vh;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	h2{
		display: flex;
	}
}
</style>

编辑 (opens new window)
#前端#vue 3.0
上次更新: 3/10/2022, 3:57:03 AM
最近更新
01
开发一个个人的cli
02-26
02
nvm-upgrade
10-11
03
git提交规范
05-01
更多文章>
Theme by Vdoing | Copyright © 2022-2022 s-xianyu | 小站
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式