Vue 3.0 生命周期函数的新写法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>lesson 41</title>
    <script src="https://unpkg.com/vue@next"></script>
    <style>
    </style>
</head>
<body>
    <div id="root"></div>
</body>
<script>
    //生命周期函数新写法
    const app = Vue.createApp({
        //beforeMount => onBeforeMount
        //mounted => onMounted
        //beforeUpdated => onBeforeUpdate
        //updated => onUpdated
        //beforeUnmount => onBeforeUnmount
        //unmouted => onUnmounted
        setup() {
            //
            const { ref, onBeforeMount, onMounted, onBeforeUpdate, 
                onUpdated, onBeforeUnmount, onUnmounted, onRenderTracked, onRenderTriggered } = Vue;
            const name = ref('dell');
            const show = ref('true');
            onBeforeMount(() => {
                console.log('onBefore');
            });
            onMounted(() => {
                console.log('onMounted');
            })
            onBeforeUpdate(() => {
                console.log('onBeforeUpdate');
            })
            onUpdated(() => {
                console.log('onUpdated');
            })
            onBeforeUnmount(() => {
                console.log('onBeforeUnmount');
            }) 
            onUnmounted(() => {
                console.log('onUnmounted');
            })
            //每次渲染后重新收集响应式依赖
            onRenderTracked(() => {
                console.log('onRenderTracked');
            }) 
            //每次触发页面重新渲染时自动执行
            onRenderTriggered(() => {
                console.log('onRenderTriggered');
            }) 
            const handleClick = () => {
                name.value = 'lee';
                show.value = !show.value;
            };
            return { name, handleClick, show }
        },  
        template:
            `
            
Hello World{{name}}
`
, }); const vm = app.mount('#root'); </script> </html>

你可能感兴趣的:(vue)