Vue.js生命周期

代码展示如下:

<!-- 
  @Author:  Mr.J
  @Date: 2019-05-05 22:31:35 
  @Last Modified by:   Mr.J
  @Last Modified time: 2019-05-05 22:31:35 
  -->

<!DOCTYPE html>
<html>

<head>
    <title>Vue生命周期</title>
    <script src="vue.js"></script>
</head>
<body>

    <div id="app">
        <p v-color="'rgb(67, 2, 128)'" v-fontsize="30">{{msg}}</p>
    </div>

    <script type="text/javascript">

        // 使用 Vue.directive()定义全局指令
        Vue.directive('color', {
            bind: function (el, binding) {
                el.style.color = binding.value;
            },
        });
        var app = new Vue({
            el: '#app',
            data: {
                msg: "Congratulation!  you have one message"
            },
            beforeCreate: function () {
                console.group('beforeCreate 创建前状态');
                console.log("%c%s", "color:red", "el     : " + this.$el); //undefined
                console.log("%c%s", "color:red", "data   : " + this.$data); //undefined 
                console.log("%c%s", "color:red", "message: " + this.message)
            },
            created: function () {
                console.group('created 创建完毕状态');
                console.log("%c%s", "color:red", "el     : " + this.$el); //undefined
                console.log("%c%s", "color:red", "data   : " + this.$data); //已被初始化 
                console.log("%c%s", "color:red", "message: " + this.message); //已被初始化
            },
            beforeMount: function () {
                console.group('beforeMount 挂载前状态');
                console.log("%c%s", "color:red", "el     : " + (this.$el)); //已被初始化
                console.log(this.$el);
                console.log("%c%s", "color:red", "data   : " + this.$data); //已被初始化  
                console.log("%c%s", "color:red", "message: " + this.message); //已被初始化  
            },
            mounted: function () {
                console.group('mounted 挂载结束状态 ');
                console.log("%c%s", "color:red", "el     : " + this.$el); //已被初始化
                console.log(this.$el);
                console.log("%c%s", "color:red", "data   : " + this.$data); //已被初始化
                console.log("%c%s", "color:red", "message: " + this.message); //已被初始化 
            },
            directives: {//自定义私有指令
                'fontsize': function (el, binding) {
                    el.style.fontSize = parseInt(binding.value) + 'px';
                },
            }
        })
    </script>
</body>

</html>

页面结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值