vue自定义指令 html,Vue11Vue自定义指令.html

自定义指令 | Vue

添加品牌

id:

Name:

Keywords:

IDNameMethod

{{ item.id }} {{ item.name }} 删除

testMessage

// 自定义指令

// 注册全局指令 v-focus

// 参数1 指令的名称 定义时候不需要 v- 的前缀,调用的时候加上前缀

// 参数2 是一个对象,包含和指令相关的函数,这些函数可以在特定的阶段执行相关的操作

// 和样式相关的最好在 bind 中设置 和 js 行为相关设置在 inserted 中

Vue.directive('focus', {

// 当被绑定元素插入到 DOM 中的时候

inserted: function (el) {

// 聚焦元素

el.focus();

},

update: function (el) {

}

})

Vue.directive('color',{

bind: function (el, binding){

el.style.color = binding.value; // 设置样式时候不需要考虑是否被插入到dom中,当组件插入到页面中的时候,肯定会解析css样式

console.log('Testing binding: ', binding.name);

console.log('Testing binding: ', binding.value);

console.log('Testing binding: ', binding.expression);

},

})

//实例化vue对象

var app = new Vue({

el: "#app",

data: {

id: "",

name: "",

keywords: "", //搜索的关键字

list: [

{ id: 1, name: "以纯" },

{ id: 2, name: "森马" },

{ id: 3, name: "真维斯" },

],

},

methods: {

add() {

var clothes = { id: this.id, name: this.name }

this.list.push(clothes)

this.id = this.name = ""

},

del(id) { //根据id删除数据

//分析,如何根据id找到要删除的索引数据

//找到索引后,直接调用数组的 splice 方法

// this.list.some((item, i)=>{

// if(item.id == id){

// this.list.splice(i,1)

// return true;

// //在数组的 some 方法中,如果return true 就会立即终止这个数组的后续循环

// }

// })

var index = this.list.findIndex(item => {

if (item.id == id) {

return true;

}

})

this.list.splice(index, 1);

},

search(keywords) { //use keywords to search data/messages

//var newList = []

// this.list.forEach(item => { //please pay attention to 'forEach', and it's structure

// if(item.name.indexOf(keywords) != -1){

// newList.push(item)

// }

// })

// return newList

// another method

// Notice: forEach、 some、 filter、 findIndex . All of these are new methods of Array

var newList = this.list.filter(item => {

// if (item.name.indexOf(keywords) != -1)

// Notice: in ES6, there is a new method of type string, and name is

// string.prototype.includes('The string to include') if include then return true

if (item.name.includes(keywords)) {

return item

}

})

return newList

//或者直接 return this.list.filter() 方法

}

},

// 局部自定义指令,只在当前定义的组件中使用

directives: {

'fontweight': { // 指令名称

// 指令的定义

bind: function(el, binding){

el.style.fontWeight = binding.value;

}

},

'fontsize': function (el, binding) { // 注意这个function 等同于把代码写道 bind 和 update 中去。

el.style.fontSize = parseInt(binding.value) + "px";

}

},

})

//Vue的过滤器

//Vue.filter('过滤器的名称',)

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值