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,所以,希望前辈能多加指点。


  • 2017-04-07 23:18:29

    mac 安装composer

    当你下载了 composer.phar 后,可以将它放在目录中,但每次当你建立新目录时,你必須再复制一个副本到新目录中,这样比较麻烦。所以最佳做法是将它放到 usr/local/bin 目录中中,成为全域指令。

  • 2017-04-12 10:52:35

    最全面的Android Webview详解

    现在很多App里都内置了Web网页(Hyprid App),比如说很多电商平台,淘宝、京东、聚划算等等,如下图

  • 2017-04-12 11:22:43

    WebView你真的熟悉吗?看了才知道

    在Android手机中内置了一款高性能webkit内核浏览器,在SDK中封装为一个叫做WebView组件。下面总结一下使用webview遇到的那些事、那些坑。

  • 2017-04-12 11:28:14

    史上最全webview详解

    WebView在现在的项目中使用的频率应该还是非常高的。 我个人总觉得HTML5是一种趋势。找了一些东西,在此总结。 本篇最后有一个非常不错 的 Html5Activity 加载类,不想看的可以直接跳下载。

  • 2017-04-12 23:11:59

    Android Studio签名打包的两种方式

    给我们自己开发的app签名,就代表着我自己的版权,以后要进行升级,也必须要使用相同的签名才行。签名就代表着自己的身份(即keystore),多个app可以使用同一个签名。

  • 2017-04-13 10:57:08

    webstorm 怎么关闭jshint

    You need to configure the inspections in Settings/Editor/Inspections, then in the list on the right, find JavaScript/JavaScript validity issues. That worked for me to remove the Chai warnings. (I am using the WebStorm 11 EAP at the moment.)

  • 2017-04-13 14:11:14

    MySQL中的describe关键字

    今天写代码的时候,有一个类的数据始终不能插入,老是提示在You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'describe, picPath1, picPath2, picPath3, picPath4, picPath5, agentId, belongStore' at line 1。因为用的是SSH进行插入,所以SQL语句不用自己写,检查了每个字段也没写错。