How to implement directive function in vue
This article introduces the simple implementation of directive, mainly learning its implementation ideas and code design. Friends who need it can refer to it
The first plan in 2018 is to learn the vue source code. After consulting some information , decided to start from the first commit, this will be a protracted battle! This article introduces the simple implementation of directive, mainly learning its implementation ideas and code design (directive and filter are very convenient to expand and comply with the "opening and closing principle" in the design pattern).
Concept API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
The implementation function should be simple - bind the data in the scope to the app.
Core logic design
Instruction format
With sd- text="msg | capitalize" Take as an example:
sd is the unified prefix identifier
text is the command name
capitalize is the filter name
where | is followed by the filter, and multiple filters can be added. sd-class-red red in
is a parameter.
Introduction to code structure
main.js entry file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Only these three files, including directives and filters. is a configuration file and is easily extensible.
The general idea of implementation is as follows:
1. When the Seed instance is created, the instructions of the node nodes in the el container will be parsed in sequence
2. The instruction parsing results will be encapsulated as Instruction object, the structure is:
##attrOriginal attributes, such as Stringkeycorresponds to the attribute name in the scope objectStringfiltersFilter name listArrayAttribute | Description | Type |
---|---|---|
sd-text
| ||
definition | The definition of this instruction, such as the function corresponding to text | Function |
argument | From Parameters parsed in attr (only supports one parameter) | String |
update | Called when updating directivetypeof def === 'function' ? def : def.update | Function |
bind | If the bind method is defined in the directive, then bindDirective will call | Function |
el | 存储当前element元素 | Element |
3.想办法执行指令的update方法即可,该插件使用了 Object.defineProperty 来定义scope中的每个属性,在其setter中触发指令的update方法。
核心代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
可以看到核心方法 processNode 主要做了两件事一个是 parseDirective ,另一个是 bindDirective 。
先来看看 parseDirective 方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
以 sd-on-click="toggle | .button" 为例来说明,其中attr对象的name为 sd-on-click ,value为 toggle | .button ,最终解析结果为:
1 2 3 4 5 6 7 8 |
|
紧接着调用 bindDirective 方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
其中的bindings存放了数据和指令的关系,该对象中的key为opts.scope中的属性,value为Object,如下:
1 2 3 4 5 6 |
|
数据与directive建立好关系之后, bindAccessors 中为seed的scope对象的属性重新定义了getter和setter,其中setter会调用指令update方法,到此就已经完事具备了。
Seed构造函数在实例化的最后会迭代bindings中的key,然后从opts.scope找到对应的value, 赋值给了scope对象,此时setter中的update就触发执行了。
下面再看一下 sd-on 指令的定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
发现它有customFilter,其实在 applyFilters 中就是针对该指令做的一个单独的判断,其中的selectors就是[".button"],最终返回一个匿名函数(事件监听函数),该匿名函数当做value传递给update方法,被其handler接收,update方法处理的是事件的绑定。这里其实实现的是事件的代理功能,customFilter中将handler包装一层作为事件的监听函数,同时还实现事件代理功能,设计的比较巧妙!
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
The above is the detailed content of How to implement directive function in vue. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values for each element; and the .map method can convert array elements into new arrays.

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.
