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

Detailed explanation of Vue Mixin function use cases

php中世界最好的语言
Release: 2018-06-08 10:47:43
Original
1711 people have browsed it

This time I will bring you a detailed explanation of the Vue Mixin function usage case. What are the precautions for the Vue Mixin function usage case? The following is a practical case, let’s take a look.

vuejs official organization has a 'vue-class-component' and the recommended 'vue-property-decorator', but there is no corresponding implementation. After looking through the former issue, there is a feature that has been pending for a long time and is mixin support.

It’s not a complicated thing, just write one yourself.

Later note: vue-class-component 6.2.0 starts to provide mixins method, which is similar to the implementation idea of ​​this article.

Implementation

Declare VClass as a class constructor for T. At the same time, the static methods (extend/mixin and the like) on Vue's constructor are obtained through Pick, so that the real implementation in the following paragraph can be supported. A new subclass is generated by calling the extend method on a Vue subclass constructor. Class constructor.

As for ABC, this is purely a manual job of type declaration.

Usage

In actual use:

Notice that the DisposableMixin that directly extends Vue is not a valid Vue component, nor can it be used directly Used in the mixins option, if it is to be used by a custom component extended by Vue.extend, remember to use Component to wrap it up.

Abstract class

Mixins used in business systems are actually more complex in most cases, providing some basic functions, but some parts need to be left to the successors to implement themselves. At this time Using abstract classes is appropriate.

But abstract classes cannot be instantiated and do not meet the requirement of { new(): T }, so they can only be inherited and cannot be mixed in. Due to the same The reason is that abstract classes cannot be decorated by the Component function of 'vue-class-component'.

At this time, we have to write the implemented functions into Mixin, and put the functions to be implemented into the interface and let the specific class implement them.

This way of deceiving the compiler is actually quite clumsy. If a specific class inherits PlayerMixin but does not explicitly declare to implement IPlayerImplementation, the compiler cannot tell you this error. We can only carefully write comments in the code and hope that users will not forget this.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Progressbar component practical case analysis

Implementation of address book initial letter search function

The above is the detailed content of Detailed explanation of Vue Mixin function use cases. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!