Vue.directive使用注意

2019-10-19 16:35:45

参考地址 Vue.directive使用注意

首先,Vue.directive要在实例初始化之前,不然会报错,还有,定义的指令不支持驼峰式写法,也会报下面同样的错,虽然在源码中没有找到在哪里统一处理大小写,但是在有关directive的方法中捕捉到的指令命名统一变为小写,所以,还是用'-'或者'_'分割吧。

vue.js:491 [Vue warn]: Failed to resolve directive: xxx

然后,其定义方式有两种,一种是Vue.directive('xxx', function(el, bind, vNode){}),其中el为dom,vNode为vue的虚拟dom,bind为一较复杂对象,详情可见Vue-directive钩子函数中的参数的参数,还有一种,为

Vue.directive('my-directive', {
  bind: function () {},
  inserted: function () {},
  update: function () {},
  componentUpdated: function () {},
  unbind: function () {}
})

参数代表的含义,参见钩子函数描述

最后,要使用自定义的指令,只需在对用的元素中,加上'v-'的前缀形成类似于内部指令'v-if','v-text'的形式。

// Vue.directive
            Vue.directive('test_directive', function(el, bind, vNode){
                el.style.color = bind.value;
            });            var app = new Vue({
                el: '#app',
                data: {
                    num: 10,
                    color: 'red'
                },
                methods: {
                    add: function(){                        this.num++;
                    }
                }
            });

当然,也可以将method中的方法加入,bind.value即为methods中的方法。

<div id="app">
            <div v-test_directive="changeColor">{{num}}</div>
            <button @click="add">增加</button>
        </div>

        <script type="text/javascript">
            // Vue.directive
            Vue.directive('test_directive', function(el, bind, vNode){
                el.style.color = bind.value();
            });            var app = new Vue({
                el: '#app',
                data: {
                    num: 10,
                    color: 'red'
                },
                methods: {
                    add: function(){                        this.num++;
                    },
                    changeColor: function(){                        return this.color;
                    }
                }
            });

这种形式,可以模仿'v-once',并进行一定的复杂逻辑,但是想要完全达到'v-once',可能需要考虑Vue-directive的钩子函数各个周期。下面是固定num的值,使得add的方法无效。

<div id="app">
            <div v-test_directive="changeColor">{{num}}</div>
            <button @click="add">增加</button>
        </div>

        <script type="text/javascript">
            // Vue.directive
            Vue.directive('test_directive', function(el, bind, vNode){
                el.style.color = bind.value();
            });            var app = new Vue({
                el: '#app',
                data: {
                    num: 10,
                    color: 'red'
                },
                methods: {
                    add: function(){                        this.num++;
                    },
                    changeColor: function(){                        this.num = 20;                        return this.color;
                    }
                }
            });

因为小生刚刚接触vue,所以,希望前辈能多加指点。


  • 2020-02-29 20:47:34

    Nuxt 特有函数和变量

    asyncData方法使得你能够在渲染组件之前异步获取数据。该方法在服务端中执行的,所以,请求数据时,不存在跨域问题。返回的数据将与 data() 返回的数据进行合并。由于asyncData方法是在组件 初始化 前被调用的,所以在方法内是没有办法通过 this 来引用组件的实例对象。

  • 2020-03-01 19:00:46

    触发onclick事件元素的获取

    自动生成元素的onclick事件 event.target返回触发事件的元素 event.currentTarget返回绑定事件的元素

  • 2020-03-03 09:46:42

    JS实现HTML标签转义及反转义

    简单说一下业务场景,前台用户通过input输入内容,在离开焦点时,将内容在div中显示。 这时遇到一个问题,如果用户输入了html标签,则在div显示中,标签被解析。 由于是纯前端操作,不涉及后端,因此需要通过js对输入内容进行转义。

  • 2020-03-03 09:51:38

    写一个可插入自定义标签的 Textarea 组件

    为了实现这个功能,我最先想的是改造一个 <textarea> 然后我想到了 contenteditable (链接指向 mozilla.org) 这一属性 这是一个 html5 的属性,可以让元素内容可编辑