Home Web Front-end JS Tutorial Found an issue with custom directives in Vue.directive

Found an issue with custom directives in Vue.directive

Jun 01, 2018 pm 03:04 PM
instruction customize

This article mainly introduces a summary of the problems with Vue.directive custom instructions. Friends in need can refer to it

1. Today I reviewed the code of Vue custom instructions, and the result was a very speechless result. , post the code first.

2.

<p id="example" v-change-by="myColor"></p>
  <script src="vue.min.js"></script>
  <script>
    new Vue({
      el:"#example",
      data:{
        msg:"",
        myColor:"#000"
      }
    });
    Vue.directive("change-by",{
      bind:(el,binding)=>{
        el.style.background=binding.value;
      }
    });
  </script>
Copy after login

3. When the page was opened, it was blank, the width and height were set, and the p did not turn black. Check that the code is correct and there are no syntax errors. Then I considered whether it was a problem with the vue.min.js file, and then downloaded the development version from the official website and used vue.js. The result was a surprising discovery.

#4. It turns out that the production version of vue.min.js does not support error reporting, which is really a pitfall!

5. Finally understood the reason, and it is very important that the instruction must be written in front of the vue instantiation object, otherwise an error will be reported as Failed to resolve directive; finally, the correct sequence code will be posted

<p id="example" v-change-by="myColor"></p>
  <script>
    Vue.directive("change-by",{
      bind:(el,binding)=>{
        el.style.background=binding.value;
      }
    });
    new Vue({
      el:"#example",
      data:{
        msg:"",
        myColor:"#000"
      }
    });
  </script>
Copy after login

6. The final output page is perfect... small issues to pay attention to.

#The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

detailed explanation of js building a binary tree for deduplication and optimization of numerical arrays

detailed explanation of Vue’s global introduction of bass .scss solution

Using node to create your own command line tool tutorial

The above is the detailed content of Found an issue with custom directives in Vue.directive. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get items using commands in Terraria? -How to collect items in Terraria? How to get items using commands in Terraria? -How to collect items in Terraria? Mar 19, 2024 am 08:13 AM

How to get items using commands in Terraria? -How to collect items in Terraria?

How to quickly set up a custom avatar in Netflix How to quickly set up a custom avatar in Netflix Feb 19, 2024 pm 06:33 PM

How to quickly set up a custom avatar in Netflix

How to customize background image in Win11 How to customize background image in Win11 Jun 30, 2023 pm 08:45 PM

How to customize background image in Win11

How to create and customize Venn diagrams in Python? How to create and customize Venn diagrams in Python? Sep 14, 2023 pm 02:37 PM

How to create and customize Venn diagrams in Python?

VUE3 quick start: using Vue.js instructions to switch tabs VUE3 quick start: using Vue.js instructions to switch tabs Jun 15, 2023 pm 11:45 PM

VUE3 quick start: using Vue.js instructions to switch tabs

How to create custom pagination in CakePHP? How to create custom pagination in CakePHP? Jun 04, 2023 am 08:32 AM

How to create custom pagination in CakePHP?

render function in Vue3: custom rendering function render function in Vue3: custom rendering function Jun 18, 2023 pm 06:43 PM

render function in Vue3: custom rendering function

How to implement custom middleware in CodeIgniter How to implement custom middleware in CodeIgniter Jul 29, 2023 am 10:53 AM

How to implement custom middleware in CodeIgniter

See all articles