自定义指令函数传参

环境:

vue@2.6.6

问题:

  1. 绑定函数传参
  2. 传参多个多样
  3. 参数动态变化

方案:

1. 采用传对象方式(binding.value)
<el-button v-mydirective="{ fn: myFn, args: x }" type="text" size="small">测试</el-button>


const mydirective = {
  inserted: function (el, binding) {
    const args = binding.value.args;
	const fn = binding.value.fn;
	el.onclick = fn(args);
  }
}
export default mydirective

以上解决了问题1. 绑定函数传参,对于问题2. 传参多个多样可以给args一个对象,或者用数组

<el-button v-mydirective="{ fn: myFn, args: {x, y} }" type="text" size="small">测试</el-button>
<script>
export default {
    data() {
       return {
           x: 1,
           y: 2,
       } 
    },
    methods: {
        myFn({x, y}) {
            console.log(x);
            console.log(y);
        }
    }
}
</script>

const mydirective = {
  inserted: function (el, binding) {
    const args = binding.value.args;
	const fn = binding.value.fn;
	el.onclick = fn(args);
  }
}
export default mydirective;
<el-button v-mydirective="{ fn: myFn, args: [x, y] }" type="text" size="small">测试</el-button>
<script>
export default {
    data() {
       return {
           x: 1,
           y: 2,
       } 
    },
    methods: {
        myFn(x, y) {
            console.log(x);
            console.log(y);
        }
    }
}
</script>

const mydirective = {
  inserted: function (el, binding) {
    const args = binding.value.args;
	const fn = binding.value.fn;
	el.onclick = fn(...args);
  }
}
export default mydirective;

对于问题3. 参数动态变化,需要增加钩子函数update或者componentUpdated

<el-button v-mydirective="{ fn: myFn, args: [x, y] }" type="text" size="small">测试</el-button>
<script>
export default {
    data() {
       return {
           x: 1,
           y: 2,
       } 
    },
    methods: {
        myFn(x, y) {
            console.log(x);
            console.log(y);
        }
    }
}
</script>

const mydirective = {
  inserted: function (el, binding) {
    const args = binding.value.args;
	const fn = binding.value.fn;
	el.onclick = fn(...args);
  },
  update: function (el, binding) {
    const args = binding.value.args;
	const fn = binding.value.fn;
	el.onclick = fn(...args);
  },
}
export default mydirective;

update时候重置绑定的onclick函数,不能使用addEventListenerremoveEventListener,因为移除不成功会导致执行两次。

2. 指令的参数(binding.arg)
<el-button v-mydirective:[x]="myFn" type="text" size="small">测试</el-button>


const mydirective = {
  inserted: function (el, binding) {
    const args = binding.arg;
	const fn = binding.value;
	el.onclick = fn(args);
  }
}
export default mydirective

以上解决了问题1. 绑定函数传参,对于问题2. 传参多个多样可以给指令参数一个对象,或者用数组

<el-button v-mydirective:[{x,y}]="myFn" type="text" size="small">测试</el-button>
<script>
export default {
    data() {
       return {
           x: 1,
           y: 2,
       } 
    },
    methods: {
        myFn({x, y}) {
            console.log(x);
            console.log(y);
        }
    }
}
</script>

const mydirective = {
  inserted: function (el, binding) {
    const args = binding.arg;
	const fn = binding.value;
	el.onclick = fn(args);
  }
}
export default mydirective;
<el-button v-mydirective:[[x,y]]="myFn" type="text" size="small">测试</el-button>
<script>
export default {
    data() {
       return {
           x: 1,
           y: 2,
       } 
    },
    methods: {
        myFn(x, y) {
            console.log(x);
            console.log(y);
        }
    }
}
</script>

const mydirective = {
  inserted: function (el, binding) {
    const args = binding.arg;
	const fn = binding.value;
	el.onclick = fn(...args);
  }
}
export default mydirective;

对于问题3. 参数动态变化,需要增加钩子函数update或者componentUpdated

<el-button v-mydirective:[[x,y]]="myFn" type="text" size="small">测试</el-button>
<script>
export default {
    data() {
       return {
           x: 1,
           y: 2,
       } 
    },
    methods: {
        myFn(x, y) {
            console.log(x);
            console.log(y);
        }
    }
}
</script>

const mydirective = {
  inserted: function (el, binding) {
    const args = binding.arg;
	const fn = binding.value;
	el.onclick = fn(...args);
  },
  update: function (el, binding) {
    const args = binding.arg;
	const fn = binding.value;
	el.onclick = fn(...args);
  },
}
export default mydirective;

update时候重置绑定的onclick函数,不能使用addEventListenerremoveEventListener,因为移除不成功会导致执行两次。与第一种类似

注意:

  • 指令参数中不能有空格
  • 指令参数中不能有字符串(需要在data中定义成变量才可以使用)
3. 采用传箭头函数(binding.value)
<el-button v-mydirective="() => myFn(x)" type="text" size="small">测试</el-button>


const mydirective = {
  inserted: function (el, binding) {
	const fn = binding.value;
	el.onclick = fn;
  }
}
export default mydirective

以上解决了问题1. 绑定函数传参问题3. 参数动态变化,对于问题2. 传参多个多样可以很随意了

<el-button v-mydirective="() => myFn({x,y})" type="text" size="small">测试</el-button>
<script>
export default {
    data() {
       return {
           x: 1,
           y: 2,
       } 
    },
    methods: {
        myFn({x, y}) {
            console.log(x);
            console.log(y);
        }
    }
}
</script>

const mydirective = {
  inserted: function (el, binding) {
	const fn = binding.value;
	el.onclick = fn;
  }
}
export default mydirective;
<el-button v-mydirective="() => myFn(x,y)" type="text" size="small">测试</el-button>
<script>
export default {
    data() {
       return {
           x: 1,
           y: 2,
       } 
    },
    methods: {
        myFn(x, y) {
            console.log(x);
            console.log(y);
        }
    }
}
</script>

const mydirective = {
  inserted: function (el, binding) {
	const fn = binding.value;
	el.onclick = fn;
  }
}
export default mydirective;

注意

  • 可以使用addEventListener,但是其他生命周期里面removeEventListener移除不成功
  • 要想移除事件可以在unbind生命周期里面el.onclick=null;,此办法并不能移除addEventListener的事件
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值