今天的在read code 的时候,发现为什么有的同事要在methods定义的函数调用的时候加上括号,而自己平时用的时候并没有这一习惯,除非要进行传参的时候才加上括号,例如:
@click='getList(id)',但是为什么有时候明明没有传参的需要,却要加上()呢? 百思不得其解,于是去查阅了相关的资料,意思就是,当不加括号直接调用这个函数是可以直接获取到这个事件对象的,加了()要想获取这个事件对象就要手动的加$event ,具体的实现是这样的:
首先是不加括号的示范
(在html 中调用)
![64fdd5f28daae8f3ff96797e406642ca.png](https://img-blog.csdnimg.cn/img_convert/64fdd5f28daae8f3ff96797e406642ca.png)
定义
![beba3d175248f8f5889bf5a7cc1eb252.png](https://img-blog.csdnimg.cn/img_convert/beba3d175248f8f5889bf5a7cc1eb252.png)
然后在控制台中打印的结果是
![5f3d69baa4c5f92ce21e5d548af02893.png](https://img-blog.csdnimg.cn/img_convert/5f3d69baa4c5f92ce21e5d548af02893.png)
就是说它这样是可以直接获取到这个事件对象的,
而另外一种情况加括号
![955db49505b1e204daefd321ee6c9f37.png](https://img-blog.csdnimg.cn/img_convert/955db49505b1e204daefd321ee6c9f37.png)
然鹅打印的时候,你会发现是undefined ....
![43c43c6da32fce2a37c2b76105a4f4f6.png](https://img-blog.csdnimg.cn/img_convert/43c43c6da32fce2a37c2b76105a4f4f6.png)
所以,此时你加了(),而又要获取这个事件对象的话,你就要手动加上$event,
![7ac9e623b17149fbf701a0a9185c3539.png](https://img-blog.csdnimg.cn/img_convert/7ac9e623b17149fbf701a0a9185c3539.png)
像这样,你同样的能获取到
![8c89a1a84cf02397c78d6e9db6a5ca9a.png](https://img-blog.csdnimg.cn/img_convert/8c89a1a84cf02397c78d6e9db6a5ca9a.png)
还有另外一种情况就是,你需要传参
![f33954c083b42ef3202dce45c6a99d3e.png](https://img-blog.csdnimg.cn/img_convert/f33954c083b42ef3202dce45c6a99d3e.png)
我要将livall 传过去
![9946d4f820f9a3d8be3099886e53083b.png](https://img-blog.csdnimg.cn/img_convert/9946d4f820f9a3d8be3099886e53083b.png)
结果是
![2aae51d39340b91fd5e497e599a9cb9d.png](https://img-blog.csdnimg.cn/img_convert/2aae51d39340b91fd5e497e599a9cb9d.png)