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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中的自定义指令生命周期发生了一些变化。首先,钩子函数的名称有所变化。在Vue2中,常用的钩子函数有bind、inserted、update、componentUpdated和unbind。而在Vue3中,这些钩子函数的名称发生了变化,分别是beforeMount、mounted、beforeUpdate、updated和unmounted。这样的改变使得自定义指令生命周期Vue3的组合式API更加一致,提高了代码的可读性和可维护性。 其次,钩子函数的参数也有所变化。在Vue2中,钩子函数的参数是el、binding、vnode和oldVnode。而在Vue3中,钩子函数的参数是el、binding、vnode和prevVNode。其中,el是当前元素,binding是指令的绑定值,vnode是虚拟节点,prevVNode是之前的虚拟节点。 总结一下,Vue3中的自定义指令生命周期发生了名称和参数的变化,名称变化使其更与Vue3的组合式API一致,参数变化则更加符合统一的规范。这些改变提高了代码的可读性和可维护性,使开发更加方便和高效。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span> #### 引用[.reference_title] - *1* *4* [Vue3中自定义指令生命周期的变化及含义?](https://blog.csdn.net/m0_68009075/article/details/130883408)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [(详解)Vue3自定义指令](https://blog.csdn.net/qq_59747594/article/details/130916986)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [对Vue2 自定义全局指令Vue.directive和指令的生命周期介绍](https://download.csdn.net/download/weixin_38743084/12953228)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值