zoukankan      html  css  js  c++  java
  • vue自定义指令directives使用及生命周期

    生命周期

    1. bind:只调用一次,指令第一次绑定到元素时调用,用这个钩子函数可以定义一个绑定时执行一次的初始化动作。
    2. inserted:被绑定元素插入父节点时调用(父节点存在即可调用,不必存在于document中)。
    3. update:被绑定于元素所在的模板更新时调用,而无论绑定值是否变化。通过比较更新前后的绑定值,可以忽略不必要的模板更新。
    4. componentUpdated:被绑定元素所在模板完成一次更新周期时调用。
    5. unbind:只调用一次,指令与元素解绑时调用。

      <div>

                  <button @click="handleClick">clickMe</button>
                   <h1 v-if="count < 6" v-change="count">it is a custom directive</h1>

     <div>

    <script>

    new Vue({
    el: '#app',
       data: {
       msg: 'Hello Vue',
       name: 'Michael',
       count: 0
      },
    directives: {
    change: {
    componentUpdated: function (el, bindings) {
    console.log('指令已经绑定到元素了');
    console.log(el);
    console.log(bindings);
    //准备将传递来的参数
    // 显示在调用该指令的元素的innerHTML
    el.innerHTML = bindings.value;

    if (bindings.value > 3) {
    el.style.color = 'red'
    }
    },
    update: function (el, bindings) {
    console.log('指令的数据有所变化');
    console.log(el);
    console.log(bindings);
    el.innerHTML = bindings.value;
    if (bindings.value == 5) {
    console.log(' it is a test');
    }
    },
    // unbind: function () {
    // console.log('解除绑定了');
    // }
    }
    });

    </script>

  • 相关阅读:
    腾讯云Windows Server下nodejs websocket ssl配置
    Windows下运行MapReduce程序出现Could not locate executable nullwinutils.exe in the Hadoop binaries.
    2.4寒假学习记录
    2.3寒假学习记录
    2.2寒假记录
    2.1日寒假学习记录
    DFA敏感词过滤实现
    手机号和邮箱合法性验证+焦点事件
    复选框显示隐藏
    table+分页+模糊查询
  • 原文地址:https://www.cnblogs.com/wgy0528/p/10038515.html
Copyright © 2011-2022 走看看