Home  >  Article  >  Web Front-end  >  Why are some methods in vue bright and some dark?

Why are some methods in vue bright and some dark?

下次还敢
下次还敢Original
2024-05-08 17:33:161162browse

The light and dark status of the method in Vue indicates the binding relationship between the method and the Vue instance. The light method is bound to the Vue instance and can access and respond to instance data and changes; the dark method is unbound and cannot access instance data and changes. Methods can be bound via the options API or the template compiler, and unbound in the same way.

Why are some methods in vue bright and some dark?

The reason why the method is bright and dark in Vue

In Vue, the light and dark of the method indicates the binding of the method. fixed state.

The bright method

The bright method means that the method is bound to the Vue instance. This indicates that the method can access the data and methods of the Vue instance and can respond to changes in the Vue instance.

Dark method

Dark method means that the method is not bound to the Vue instance. This indicates that the method cannot access the data and methods of the Vue instance and will not respond to changes in the Vue instance.

Binding method

Methods can be bound in two ways:

  1. By options API: In methods Methods defined in options:
<code class="js">export default {
  methods: {
    myMethod() {
      // ...
    }
  }
}</code>
  1. Via the template compiler: Use the v-on directive in the template :
<code class="html"><button @click="myMethod">...</button></code>

Unbinding method

You can unbind the method in the following ways:

  1. By options API: Remove methods from methods Options:
<code class="js">export default {
  // ...
  methods: {
    // myMethod() { } 已被删除
  }
  // ...
}</code>
  1. Via template compiler: Remove from templates v-on Command:
<code class="html"><button>...</button></code>

The above is the detailed content of Why are some methods in vue bright and some dark?. 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