天天看点

vue学习笔记 slot插槽 自定义指令 生命周期实例

<!DOCTYPE html>
<html>

<head>
    <title></title>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/vue/2.2.3/vue.js"></script>
</head>

<body>

    <div id="app">
        <!-- slot插槽使用 slot属性对应组件中name属性相同的节点进行渲染 -->
        <temp>
            <span id="aa" style="color:red">
                slot
            </span>
            <span id="bb" slot="block">
                slot1
            </span>
        </temp>
        <!-- 自定义指令使用 -->
        <div v-add-style="{color:'red',text:'aaa'}">

        </div>
    </div>
    <!-- 创建插槽组件 -->
    <script type="text/html" id="template">
        <div>
		<slot></slot>
		<slot  name="block"></slot>
		<slot name="block"></slot>

		<slot name="block"></slot>
	</div>
</script>
    <script type="text/javascript">
        //添加自定义指令    
        Vue.directive('addStyle', function (el, binding) {
            el.style.backgroundColor = binding.value.color;
            el.innerText = binding.value.text;
            console.log(binding.value.color);
            console.log(binding.value.text);
        })
        //添加自定义指令 end

        // 初始化组件
        Vue.component('temp', {
            template: "#template",
            data: function () {
                return {

                }
            }
        })
        var app = new Vue({
            el: '#app',
            data: {

            },
            // vue生命周期对应
            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); //已被初始化 
            },
            beforeUpdate: function () {
                console.group('beforeUpdate 更新前状态===============》');
                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);
            },
            updated: function () {
                console.group('updated 更新完成状态===============》');
                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);
            },
            beforeDestroy: function () {
                console.group('beforeDestroy 销毁前状态===============》');
                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);
            },
            destroyed: function () {
                console.group('destroyed 销毁完成状态===============》');
                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)
            }
        })
    </script>
</body>

</html>