yaogengzhu / daily-share

个人博客记录、内容在issues
30 stars 4 forks source link

vue的生命周期函数 #10

Open yaogengzhu opened 5 years ago

yaogengzhu commented 5 years ago

组件开始创建期间的四个生命周期函数

yaogengzhu commented 5 years ago
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <p id="p">{{ msg }}</p>
    </div>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                msg: 'hello'
            },
            beforeCreate(){
                // console.log("bfc")
                console.log(document.getElementById('p').innerHTML)   
                // 方法和数据都没有被初始化      
                console.log('bfe=====' + this.msg +  '======' + this.change)
            },
            created() {
                // console.log('cre')
                console.log(document.getElementById('p').innerHTML)
                // 方法和数据已经被初始化
                console.log('bfe=====' + this.msg +  '======' + this.change)
                this.msg = '您好'
            },
            beforeMount(){
                // 数据已经被初始化,但是页面还是旧的数据,没有及时渲染
                console.log(document.getElementById('p').innerHTML)
                console.log('bfe=====' + this.msg +  '======' + this.change)
            },
            mounted(){
                // 数据已经被初始化,且渲染到页面中去了
                console.log(document.getElementById('p').innerHTML)
                console.log('bfe=====' + this.msg +  '======' + this.change)
            },
            methods: {
                change(){

                }
            }
        })
    </script>
</body>
</html>
yaogengzhu commented 5 years ago

补充两个生命周期函数

第五个生命周期函数,这个函数一般是不触发的,只有当数据更新之后才会触发!但是页面的数据还没有更新,只有在内存中才更新了数据

第六个生命周期函数

数据已经渲染到页面了,所有的数据都是最新的数据了

yaogengzhu commented 5 years ago

补充两个销毁的生命周期函数

这个函数执行时,整个实例已经完全被销毁了,已经无法操作data 和 method