Vue components bind native events

March 12, 2019 18:37:04 Comment Reading (1713)
This article was last updated on March 12, 2019, and has not been updated for more than a year. If the article content or picture resources are invalid, please leave a message for feedback, and we will deal with it in a timely manner, thank you!

Vue组件绑定原生事件

Is the vue component bound to a native event? Normally, if we want to bind an event to a component, we need to write it in this way. First, we need to trigger the event up through $emit, then listen to the event, and finally execute the method function, such as the following:

  1. <div id= "root" >
  2.         <child @click= "handleClick" ></child>
  3.     </div>
  4.     <script>
  5.         Vue.component('child',{
  6.             template: '<div @click= "handleChildClick" >child</div>',
  7.             methods: {
  8.                 handleChildClick:  function  () {
  9.                      this .$emit('click')
  10.                 }
  11.             }
  12.         })
  13.          var  vm =  new  Vue({
  14.             el: '#root',
  15.             methods: {
  16.                 handleClick:  function  () {
  17. Alert ('handleClick method ')
  18.                 }
  19.             }
  20.         })
  21.     </script>

Then click the component to execute Alert ('handleClick method ') , but I haven't found that this is troublesome. Is there a simpler way to execute it? Yes, that's it. Add a . native modifier , as follows:

  1. <div id= "root" >
  2.         <child @click. native = "handleClick" ></child>
  3.     </div>
  4.     <script>
  5.         Vue.component('child',{
  6.             template: '<div>child</div>'
  7.         })
  8.          var  vm =  new  Vue({
  9.             el: '#root',
  10.             methods: {
  11.                 handleClick:  function  () {
  12. Alert ('handleClick method ')
  13.                 }
  14.             }
  15.         })
  16.     </script>

This is a native click event, isn't it convenient?

 

 weinxin
WeChat applet
Internet development, lifelong learners, welcome your attention!
 Willing

Comment

 unhappy  rainbow  look on  Diu Xiang  Good  funny  Flower core  Cry out  surprised  blink the eyes  Cool  Sad  Are you handsome?  gift  rose  anger  get angry  spray  Sleep?  Too happy  Little 999  ah
 sunlight  Spit out one's tongue  Grievance  smiling eyes  Star Moon  heart-broken  Eh  Insidious  doubt  that 's great  Snicker  Squint  shock  slightly  Yawn  Helpless to cry  Pick one's nose  Hum  expect  I don't care about you  love  candle