Home > Web Front-end > JS Tutorial > body text

Design process of Vue.js component library event system (code)

不言
Release: 2018-09-10 17:19:46
Original
1438 people have browsed it

The content of this article is about the design process (code) of the Vue.js component library event system. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Let’s take input-number as an example:

@ is the abbreviation of v-on instruction, used to bind event listeners:

<InputNumber @on-change="change" :max="10" :min="1" v-model="value1">
</InputNumber>
Copy after login

When we use components, A custom event will be registered:

methods: {
    change (v) {
        console.log(v)
    }
}
Copy after login

The way to trigger it inside the component is also very simple:

calls $emit to trigger the current instance event, the event name is on-change
this.$emit('on-change', val);
Copy after login

Here comes the idea, if the outer layer of InputNumber is nested in a certain FormItem component, The mutual calls between events are also similar, but there is an additional assumption:

Nested relationship, there may be multiple levels of parent and child

Like element and iview More designs A mixins, which provides a method: dispatch

It accepts 3 parameters:

  • componentName Component name

  • eventName Custom event name

  • params Parameters passed by the event

dispatch(componentName, eventName, params) {
}
Copy after login

For example, similar to input-number, many of the components embedded in this form will be designed to interact with FormItem:

this.dispatch('FormItem', 'on-form-change', val);
Copy after login

We are designing FormItem When using components, note:

export default {
    name: 'FormItem'
}
Copy after login

Then register a custom event in the same way:

<Form-item @on-form-change="test">
</Form-item>
Copy after login

Let’s take a look at the inside of the dispatch function:

The idea is to look up the parent element layer by layer:

  • ##$parent -- parent instance

  • $root -- the root of the component tree Vue instance

var parent = this.$parent || this.$root;
Copy after login
Get the name of the parent component:

var name = parent.$options.name;
Copy after login
Start the loop judgment:

while (parent && (!name || name !== componentName)) {
    // ...
}
Copy after login
For example, the above

input-number Dispatch is called internally and the parameters are passed in, which is to keep looking for the parent element name which is FormItem. Inside the while:

then Find its parent example, and then get the name

parent = parent.$parent;
if (parent) {
    name = parent.$options.name;
}
Copy after login
Finally if found:

is the same as the custom event triggered at the beginning: $emit

if (parent) {
    parent.$emit.apply(parent, [eventName].concat(params));
}
Copy after login
Related recommendations:

Vue.js component communication child component to parent component communication (code)


Vue.js mobile component library usage method

The above is the detailed content of Design process of Vue.js component library event system (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template