Home  >  Article  >  Web Front-end  >  What should I do if Vue dynamically adds element click events that don’t work?

What should I do if Vue dynamically adds element click events that don’t work?

PHPz
PHPzOriginal
2023-04-11 10:29:563198browse

In Vue, many times we need to dynamically add elements to the page. However, sometimes we find that the click events of these dynamically added elements do not work. Why is this? This article will introduce you to the reasons and solutions.

1. Reason

Let’s first understand why the click event of dynamically added elements does not work. This is because Vue will monitor existing elements during the page loading process. When a click event occurs, the corresponding method on the Vue instance will be triggered. However, when we add elements dynamically, Vue does not automatically add listeners to these elements, so the click event is invalid.

2. Solution

Now that the cause has been clarified, how to solve this problem?

  1. Using Vue's event delegation mechanism

Vue has an event delegation mechanism that can listen to events on the parent element. When the child element is clicked, the event is sent through The target attribute of the object is used to determine the specific element clicked. We can use this mechanism to solve the problem of invalid click events on dynamically added elements.

For example, in our Vue component, we can write like this:

<template>
  <div class="parent" @click="handleClick">
    <div class="dynamic-element" v-for="item in list" :key="item.id">{{item.name}}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        {id: 1, name: '元素1'},
        {id: 2, name: '元素2'},
        {id: 3, name: '元素3'},
      ]
    }
  },
  methods: {
    handleClick(e) {
      if (e.target.classList.contains('dynamic-element')) {
        console.log(e.target.innerText)
      }
    }
  }
}
</script>

In the above code, we are in the parent element <div class="parent"> The click event is monitored on . When the child element <div class="dynamic-element"> is clicked, the handleClick method will be triggered. In the handleClick method, we can use the target attribute of the event object to determine which sub-element was clicked.

Since this method requires adding a listener once on the Vue instance, there may be performance issues. If you need to dynamically add elements frequently, this approach may incur high performance overhead.

  1. Using Vue’s modifiers

Another solution is Vue’s modifiers. We can use Vue's modifiers to bind events to dynamically added elements.

<template>
  <div>
    <div class="dynamic-element" v-for="item in list" :key="item.id" @click="handleClick">{{item.name}}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        {id: 1, name: '元素1'},
        {id: 2, name: '元素2'},
        {id: 3, name: '元素3'},
      ]
    }
  },
  methods: {
    handleClick() {
      console.log('点击了动态元素')
    }
  }
}
</script>

In the above code, we bound the @click event to the <div class="dynamic-element"> element and called the handleClick method. Since we have used Vue's responsive mechanism when adding elements dynamically, when we add a new element, it will automatically bind the @click event to the new element.

The advantage of this method is that it is more convenient, but it should be noted that Vue's modifiers are only available for static elements and components, and are not valid for dynamically added elements. Therefore, this method is only suitable for single rendering.

3. Conclusion

Through the above two methods, we can solve the problem of invalid click events of dynamically added elements in Vue. The above are just two common solutions. In fact, there are many other solutions. In actual development, you can choose the technical solution that best suits you based on your specific situation.

The above is the detailed content of What should I do if Vue dynamically adds element click events that don’t work?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn