1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <!--备注:v-fontColor会被解析成全部小写的fontcolor,所以自定义指令的名字要用小写--> <span v-text="msg" v-fontColor="'red'"></span> var vm = new Vue({ el:'#app', data:{ msg:"自定义私有指令fontColor", id:'', name:'', keyWords:'', list:[ {id:1,name:'奔驰',ctime:new Date()}, {id:2,name:'宝马',ctime:new Date()}, {id:3,name:'奥迪',ctime:new Date()} ] },methods:{ del(id){ console.log('delete:' + id); this.list.some((item,i) =>{ if(item.id == id){ this.list.splice(i,1); return true; } }) }, add(){ this.list.push({id:this.id,name:this.name,ctime:new Date()}); }, search(keyWords){ console.log(keyWords); var rv = []; this.list.forEach(item => { if(item.name.indexOf(keyWords) >= 0){ rv.push(item); } }); return rv; } },directives:{ 'fontcolor':{ bind:function(el,binding){ el.style.color = binding.value; } } } })
|